•  


GitHub - youknowone/ring: Python cache interface with clean API and built-in memcache & redis + asyncio support.
Skip to content

Python cache interface with clean API and built-in memcache & redis + asyncio support.

License

Notifications You must be signed in to change notification settings

youknowone/ring

Repository files navigation

Ring

Join the chat at https://gitter.im/ring-cache/community

Let's concentrate on code, not on storages.

Ring shows a way to control cache in point of view of code - not about storages. Ring's decorator is convenient but also keeps fluency for general scenarios.

asyncio support for Python3.5+!

Take advantage of perfectly explicit and fully automated cache interface. Ring decorators convert your functions to cached version of them, with extra control methods.

Documentation

Full documentation with examples and references: http://ring-cache.readthedocs.io/

  • Function/method support.
  • asyncio support.
  • Django support.
  • Bulk access support.

Function cache

import
 ring

import
 memcache

import
 requests


mc
 =
 memcache
.
Client
([
'127.0.0.1:11211'
])

# working for mc, expire in 60sec

@
ring
.
memcache
(
mc
, 
time
=
60
)

def
 get_url
(
url
):
    
return
 requests
.
get
(
url
).
content


# normal way - it is cached

data
 =
 get_url
(
'http://example.com'
)

It is a normal smart cache flow.

But ring is different when you want to explicitly control it.

# delete the cache

get_url
.
delete
(
'http://example.com'
)
# get cached data or None

data_or_none
 =
 get_url
.
get
(
'http://example.com'
)

# get internal cache key

key
 =
 get_url
.
key
(
'http://example.com'
)
# and access directly to the backend

direct_data
 =
 mc
.
get
(
key
)

Method cache

import
 ring

import
 redis


rc
 =
 redis
.
StrictRedis
()

class
 User
(
dict
):
    
def
 __ring_key__
(
self
):
        
return
 self
[
'id'
]

    
# working for rc, no expiration

    # using json coder for non-bytes cache data

    @
ring
.
redis
(
rc
, 
coder
=
'json'
)

    def
 data
(
self
):
        
return
 self
.
copy
()

    
# parameters are also ok!

    @
ring
.
redis
(
rc
, 
coder
=
'json'
)

    def
 child
(
self
, 
child_id
):
        
return
 {
'user_id'
: 
self
[
'id'
], 
'child_id'
: 
child_id
}

user
 =
 User
(
id
=
42
, 
name
=
'Ring'
)

# create and get cache

user_data
 =
 user
.
data
()  
# cached

user
[
'name'
] 
=
 'Ding'

# still cached

cached_data
 =
 user
.
data
()
assert
 user_data
 ==
 cached_data

# refresh

updated_data
 =
 user
.
data
.
update
()
assert
 user_data
 !=
 updated_data


# id is the cache key so...

user2
 =
 User
(
id
=
42
)
# still hitting the same cache

assert
 updated_data
 ==
 user2
.
data
()

Installation

PyPI is the recommended way.

$ pip install ring
To browse versions and tarballs, visit:
https://pypi.python.org/pypi/ring/

To use memcached or redis, don't forget to install related libraries. For example: python-memcached, python3-memcached, pylibmc, redis-py, Django etc

It may require to install and run related services on your system too. Look for memcached and redis for your system.

Contributors

See contributors list on:
https://github.com/youknowone/ring/graphs/contributors
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본