Skip to content

Commit

Permalink
@ring.func. -> @Ring.
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed May 7, 2018
1 parent 2863b3c commit 0791eb2
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 49 deletions.
12 changes: 7 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Function cache
mc = memcache.Client(['127.0.0.1:11211'])
# working for mc, expire in 60sec
@ring.func.memcache(mc, time=60)
@ring.memcache(mc, time=60)
def get_url(url):
return requests.get(url).content
Expand Down Expand Up @@ -68,12 +68,12 @@ Method cache
# working for rc, no expiration
# using json coder for non-bytes cache data
@ring.func.redis(rc, coder='json')
@ring.redis(rc, coder='json')
def data(self):
return self.copy()
# parameters are also ok!
@ring.func.redis(rc, coder='json')
@ring.redis(rc, coder='json')
def child(self, child_id):
return {'user_id': self['id'], 'child_id': child_id}
Expand Down Expand Up @@ -111,7 +111,8 @@ To browse versions and tarballs, visit:
To use memcache or redis, don't forget to install related libraries.
For example: python-memcached, python3-memcached, pylibmc, redis-py etc

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


Contributors
Expand All @@ -138,8 +139,9 @@ Run pytest to check the test set is ready.
$ pytest -vv
Note: Can't you install it because of compile errors?
Note: Can't you install `ring[tests]` because of compile errors?
Don't forget to install and run memcached and redis locally.
Test codes are using memcached & redis to ensure ring is correctly working.

For macOS:

Expand Down
2 changes: 1 addition & 1 deletion examples/caching_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time


@ring.func.dict({})
@ring.dict({})
def get_url(url):
"""very slow function
"""
Expand Down
9 changes: 8 additions & 1 deletion ring/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@

import ring.func # noqa
from ring.func import (
dict, memcache, redis_py, redis, disk,
aiodict, aiomcache, aioredis)


__all__ = (
'dict', 'memcache', 'redis_py', 'redis', 'disk',
'aiodict', 'aiomcache', 'aioredis')
15 changes: 10 additions & 5 deletions ring/func.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Collection of cache decorators"""

from ring.func_sync import dict, memcache, redis_py, redis, arcus, disk
from ring.func_sync import dict, memcache, redis_py, redis, disk

try:
import asyncio
except ImportError:
asyncio = False

__all__ = ('dict', 'memcache', 'redis_py', 'redis', 'aiomcache', 'aioredis', 'arcus', 'disk')


if asyncio:
from ring.func_asyncio import aiomcache, aioredis
from ring.func_asyncio import aiodict, aiomcache, aioredis
else:
aiodict = None
aiomcache = None
aioredis = None

__all__ = (
'dict', 'memcache', 'redis_py', 'redis', 'disk',
'aiodict', 'aiomcache', 'aioredis')
2 changes: 1 addition & 1 deletion ring/func_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def dict(
ignorable_keys=ignorable_keys)


async_dict = dict
aiodict = dict


def aiomcache(
Expand Down
6 changes: 3 additions & 3 deletions tests/_test_func_async_def.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import pytest
import ring


@pytest.mark.asyncio
Expand All @@ -14,7 +15,6 @@ def vanilla_function():

@pytest.mark.asyncio
async def test_async_def_func_method():
import ring.func_asyncio
cache = {}

async def async_func(n):
Expand All @@ -24,12 +24,12 @@ class A(object):
def __str__(self):
return 'A'

@ring.func_asyncio.async_dict(cache)
@ring.aiodict(cache)
async def method(self, a, b):
x = await async_func(100)
return base + a * x + b

@ring.func_asyncio.async_dict(cache)
@ring.aiodict(cache)
@classmethod
async def cmethod(cls, a, b):
x = await async_func(200)
Expand Down
10 changes: 5 additions & 5 deletions tests/_test_func_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@asyncio.coroutine
def storage_dict():
storage = StorageDict()
storage.ring = ring.func_asyncio.async_dict
storage.ring = ring.aiodict
return storage


Expand Down Expand Up @@ -147,7 +147,7 @@ def complicated(a, *args, b, **kw):
def test_func_dict():
cache = {}

@ring.func_asyncio.async_dict(cache)
@ring.aiodict(cache)
@asyncio.coroutine
def f1(a, b):
return a * 100 + b
Expand All @@ -157,7 +157,7 @@ def f1(a, b):

cache = {}

@ring.func_asyncio.async_dict(cache, expire=1)
@ring.aiodict(cache, expire=1)
@asyncio.coroutine
def f2(a, b):
return a * 100 + b
Expand All @@ -175,12 +175,12 @@ class A(object):
def __ring_key__(self):
return 'A'

@ring.func_asyncio.async_dict(storage)
@ring.aiodict(storage)
@asyncio.coroutine
def method(self, a, b):
return base + a * 100 + b

@ring.func_asyncio.async_dict(storage)
@ring.aiodict(storage)
@classmethod
@asyncio.coroutine
def cmethod(cls, a, b):
Expand Down
4 changes: 2 additions & 2 deletions tests/_test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_coder_func():

storage = {}

@ring.func.dict(storage)
@ring.dict(storage)
def f(a):
return a

Expand Down Expand Up @@ -172,7 +172,7 @@ class User(Object):
def __ring_key__(self):
return 'User{self.user_id}'.format(self=self)

@ring.func.dict(storage)
@ring.dict(storage)
def data(self):
return self._data.copy()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_action_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class User(Object):
def __ring_key__(self):
return 'User{self.user_id}'.format(self=self)

@ring.func.dict(cache)
@ring.dict(cache)
def data(self):
return self._data.copy()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_coder.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_coder_pickle():
coder = default_registry.get('pickle')
mc = memcache.Client(['127.0.0.1:11211'])

@ring.func.memcache(mc, coder='pickle')
@ring.memcache(mc, coder='pickle')
def now():
return datetime.datetime.now()

Expand All @@ -75,7 +75,7 @@ def now():


def test_ring_bare_coder():
@ring.func.dict({}, coder=JsonCoder)
@ring.dict({}, coder=JsonCoder)
def f():
return 10

Expand All @@ -86,7 +86,7 @@ def test_unexisting_coder():
cache = {}

with pytest.raises(TypeError):
@ring.func.dict(cache, coder='messed-up')
@ring.dict(cache, coder='messed-up')
def f():
pass

Expand Down
20 changes: 10 additions & 10 deletions tests/test_func_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class StorageDict(dict):
@pytest.fixture
def storage_dict():
storage = StorageDict()
storage.ring = ring.func.dict
storage.ring = ring.dict
storage.is_binary = False
storage.has_touch = True
return storage
Expand All @@ -46,7 +46,7 @@ def memcache_client(request):
pytest.skip()
client.is_binary = is_binary
client.has_touch = has_touch
client.ring = ring.func.memcache
client.ring = ring.memcache
return client


Expand All @@ -55,7 +55,7 @@ def memcache_client(request):
])
def redis_client(request):
client = request.param
client.ring = ring.func.redis
client.ring = ring.redis
client.is_binary = True
client.has_touch = True
return client
Expand All @@ -66,7 +66,7 @@ def redis_client(request):
])
def disk_cache(request):
client = request.param
client.ring = ring.func.disk
client.ring = ring.disk
client.is_binary = False
client.has_touch = False
return client
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_func_dict():

base = [0]

@ring.func.dict(cache, key_prefix='')
@ring.dict(cache, key_prefix='')
def f(a, b):
return base[0] + a * 100 + b

Expand Down Expand Up @@ -223,7 +223,7 @@ def f(a, b):
def test_func_dict_expire():
cache = {}

@ring.func.dict(cache, expire=1)
@ring.dict(cache, expire=1)
def f(a, b):
return a * 100 + b

Expand All @@ -247,7 +247,7 @@ def f(a, b):
])
def test_ring_key(value):
# test only with real cache backends. dict doesn't help this test
@ring.func.memcache(pythonmemcache_client, expire=1)
@ring.memcache(pythonmemcache_client, expire=1)
def simple(key):
return key

Expand All @@ -258,7 +258,7 @@ def simple(key):
def test_memcache(memcache_client):
base = [0]

@ring.func.memcache(memcache_client, 'ring-test')
@ring.memcache(memcache_client, 'ring-test')
def f(a, b):
r = base[0] + a * 100 + b
sr = str(r)
Expand All @@ -278,7 +278,7 @@ def f(a, b):
def test_disk(disk_cache):
base = [0]

@ring.func.disk(disk_cache, 'ring-test')
@ring.disk(disk_cache, 'ring-test')
def f(a, b):
r = base[0] + a * 100 + b
sr = str(r)
Expand All @@ -298,7 +298,7 @@ def f(a, b):
def test_redis(redis_client):
base = [0]

@ring.func.redis(redis_client, 'ring-test', 5)
@ring.redis(redis_client, 'ring-test', 5)
def f(a, b):
r = base[0] + a * 100 + b
return str(r).encode('utf-8')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ignorable_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def test_basic_ignorable_key():
@ring.func.dict({}, ignorable_keys=['ignorable'])
@ring.dict({}, ignorable_keys=['ignorable'])
def f(n, ignorable):
return n + ignorable

Expand Down
6 changes: 3 additions & 3 deletions tests/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_classmethod_key():

class A(object):

@ring.func.dict(cache)
@ring.dict(cache)
@classmethod
def f(cls):
return 10
Expand All @@ -68,7 +68,7 @@ def test_unexisting_ring_key():
cache = {}

class A(object):
@ring.func.dict(cache)
@ring.dict(cache)
def f(self):
return 0

Expand All @@ -80,7 +80,7 @@ def f(self):
@pytest.mark.parametrize('v', (None, Ellipsis))
def test_singleton(v):

@ring.func.dict({})
@ring.dict({})
def f(a, b=v):
return a, b

Expand Down
2 changes: 1 addition & 1 deletion tests/test_memcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_memcache_key(client):
if client is None:
pytest.skip()

@ring.func.memcache(client, 'ring-test')
@ring.memcache(client, 'ring-test')
def f(a, b):
r = a + b
if isinstance(r, str):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_memoize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def test_fibonacci():
cache = {}

@ring.func.dict(cache, ignorable_keys=['use_cache'])
@ring.dict(cache, ignorable_keys=['use_cache'])
def fibonacci(n, use_cache):
if use_cache:
raise RuntimeError('it is not cached!')
Expand Down
6 changes: 3 additions & 3 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_readme_function():
mc = memcache.Client(['127.0.0.1:11211'])

# working for mc, expire in 60sec
@ring.func.memcache(mc, expire=60)
@ring.memcache(mc, expire=60)
def get_url(url):
return requests.get(url).content

Expand Down Expand Up @@ -42,12 +42,12 @@ def __ring_key__(self):

# working for rc, no expiration
# using json coder to cache and load
@ring.func.redis(rc, coder='json')
@ring.redis(rc, coder='json')
def data(self):
return self.copy()

# parameters are also ok!
@ring.func.redis(rc, coder='json')
@ring.redis(rc, coder='json')
def child(self, child_id):
return {'user_id': self['id'], 'child_id': child_id}

Expand Down
Loading

0 comments on commit 0791eb2

Please sign in to comment.