Skip to content

Commit

Permalink
Refactor Ring & RingBase
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed May 20, 2018
1 parent a67cfba commit 21e3b39
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 152 deletions.
12 changes: 6 additions & 6 deletions docs/control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ which give elaborate controlling. Note that `object` in `Ring object` doesn't
mean Python object. They are a collection of Ring-interface-injected stuff
which shares interface described in this document.

Though it is up to factory functions :func:`ring.func_sync.ring_factory`
and :func:`ring.func_asyncio.ring_factory`, they share following features.
Though it is up to factory functions :func:`ring.func_sync.ring_class_factory`
and :func:`ring.func_asyncio.ring_class_factory`, they share following features.


Meta controller
Expand Down Expand Up @@ -52,7 +52,7 @@ Building blocks
def f():
...
assert f.ring.storage is storage
assert f.storage is storage
.. code-block:: python
Expand All @@ -62,7 +62,7 @@ Building blocks
def f():
...
assert f.ring.storage is client
assert f.storage is client
.. function:: decode(cache_data)
Expand All @@ -85,7 +85,7 @@ Building blocks
r1 = f.get()
# storage.get may vary by actual storage object
r2 = f.decode(f.ring.storage.get(f.key()))
r2 = f.decode(f.storage.get(f.key()))
assert r1 == r2
Expand Down Expand Up @@ -114,7 +114,7 @@ Building blocks
f.set(f.encode(result))
# way #3
# storage.set may vary by actual storage object
f.ring.storage.set(f.key(), f.encode(result))
f.storage.set(f.key(), f.encode(result))
Cache behavior controller
Expand Down
2 changes: 1 addition & 1 deletion docs/factory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Common factory parameters
object. See :doc:`coder` for details.
:param List[str] ignorable_keys: (unstable) Parameter names not to use to
create cache key.
:param Union[ring.func_sync.CacheInterface,ring.func_asyncio.ring.func_sync.CacheInterface] interface:
:param Union[ring.func_sync.CacheInterface,ring.func_asyncio.CacheInterface] interface:
Injective implementation of sub-functions.
:param ring.func_base.StorageImplementation storage_implementation:
Injective implementation of storage.
Expand Down
6 changes: 3 additions & 3 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The core feature of **Ring** is explicit controllers.
# get internal cache key
key = get_url.key('http://example.com')
# and access directly to the backend
encoded_data = get_url.ring.storage.get(key)
encoded_data = get_url.storage.get(key)
cached_data = get_url.decode(encoded_data)
Expand Down Expand Up @@ -230,7 +230,7 @@ decides the kind of coding.
loaded = f.get()
assert isinstance(loaded, dict)
assert loaded == {'key': 'data', 'number': 42}
raw_data = f.ring.storage.get(f.key())
raw_data = f.storage.get(f.key())
assert isinstance(raw_data, bytes) # `str` for py2
assert raw_data == json.dumps({'key': 'data', 'number': 42})
Expand Down Expand Up @@ -268,7 +268,7 @@ don't need to be suffered by looking inside of **Ring**.
...
key = f.key() # retrieving the key
raw_data = f.ring.storage.get(key) # getting raw data from storage
raw_data = f.storage.get(key) # getting raw data from storage
:see: :doc:`control` for more attributes.
Expand Down
4 changes: 2 additions & 2 deletions docs/why.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ objects serve data extractors instead.
...
cache_key = f.key(10) # cache key for 10
assert f.ring.storage is client
encoded_data = f.ring.storage.get(cache_key) # get from memcache client
assert f.storage is client
encoded_data = f.storage.get(cache_key) # get from memcache client
actual_data = f.decode(encoded_data) # decode
:see: :doc:`control` for details.
Expand Down
4 changes: 2 additions & 2 deletions ring/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import functools
from django.core import cache
from ring import func_base as fbase
from ring.func_sync import ring_factory, CacheInterface
from ring.func_sync import ring_class_factory, CacheInterface


__all__ = ('django', 'django_default')
Expand Down Expand Up @@ -49,7 +49,7 @@ def django(
"""
backend = promote_backend(backend)
return fbase.factory(
backend, key_prefix=key_prefix, ring_factory=ring_factory,
backend, key_prefix=key_prefix, ring_class_factory=ring_class_factory,
interface=interface, storage_implementation=storage_implementation,
miss_value=None, expire_default=expire, coder=coder,
ignorable_keys=ignorable_keys)
Expand Down
78 changes: 38 additions & 40 deletions ring/func_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,53 @@
inspect, 'iscoroutinefunction', lambda f: False)


def ring_factory(
c, storage_instance, ckey, RingBase,
miss_value, expire_default, coder):
def ring_class_factory(cwrapper):

if not c.is_coroutine:
if not cwrapper.is_coroutine:
raise TypeError(
"The function for cache '{}' must be an async function.".format(
c.code.co_name))

_coder = coder

class Ring(RingBase):
_callable = c
_ckey = ckey
_expire_default = expire_default

storage = storage_instance
_miss_value = miss_value

coder = _coder
cwrapper.code.co_name))

class Ring(object):
# primary primitive methods

@asyncio.coroutine
def _p_execute(self, kwargs):
result = yield from self._callable.callable(*self.wire._preargs, **kwargs)
def execute(self, kwargs):
result = yield from self.cwrapper.callable(
*self.wire._preargs, **kwargs)
return result

@asyncio.coroutine
def _p_get(self, key):
value = yield from self.storage_impl.get_value(self.storage, key)
def storage_get(self, key):
value = yield from self.storage_impl.get_value(
self.storage, key)
return self.coder.decode(value)

@asyncio.coroutine
def _p_set(self, key, value, expire=_expire_default):
def storage_set(self, key, value, expire=...):
if expire is ...:
expire = self.expire_default
encoded = self.coder.encode(value)
result = yield from self.storage_impl.set_value(self.storage, key, encoded, expire)
result = yield from self.storage_impl.set_value(
self.storage, key, encoded, expire)
return result

@asyncio.coroutine
def _p_delete(self, key):
result = yield from self.storage_impl.del_value(self.storage, key)
def storage_delete(self, key):
result = yield from self.storage_impl.del_value(
self.storage, key)
return result

@asyncio.coroutine
def _p_touch(self, key, expire=expire_default):
result = yield from self.storage_impl.touch_value(self.storage, key, expire)
def storage_touch(self, key, expire=...):
if expire is ...:
expire = self.expire_default
result = yield from self.storage_impl.touch_value(
self.storage, key, expire)
return result

fbase.Ring.register(Ring)

return Ring


Expand All @@ -70,43 +68,43 @@ class CacheInterface(fbase.BaseInterface):
def get(self, **kwargs):
key = self.key(**kwargs)
try:
result = yield from self.ring._p_get(key)
result = yield from self.ring.storage_get(key)
except fbase.NotFound:
result = self.ring._miss_value
result = self.ring.miss_value
return result

@asyncio.coroutine
def update(self, **kwargs):
key = self.key(**kwargs)
result = yield from self.ring._p_execute(kwargs)
yield from self.ring._p_set(key, result, self.ring._expire_default)
result = yield from self.ring.execute(kwargs)
yield from self.ring.storage_set(key, result)
return result

@asyncio.coroutine
def get_or_update(self, **kwargs):
key = self.key(**kwargs)
try:
result = yield from self.ring._p_get(key)
result = yield from self.ring.storage_get(key)
except fbase.NotFound:
result = yield from self.ring._p_execute(kwargs)
yield from self.ring._p_set(key, result, self.ring._expire_default)
result = yield from self.ring.execute(kwargs)
yield from self.ring.storage_set(key, result)
return result

@asyncio.coroutine
def set(self, value, **kwargs):
key = self.key(**kwargs)
yield from self.ring._p_set(key, value, self.ring._expire_default)
yield from self.ring.storage_set(key, value)
set._function_args_count = 1

@asyncio.coroutine
def delete(self, **kwargs):
key = self.key(**kwargs)
yield from self.ring._p_delete(key)
yield from self.ring.storage_delete(key)

@asyncio.coroutine
def touch(self, **kwargs):
key = self.key(**kwargs)
yield from self.ring._p_touch(key)
yield from self.ring.storage_touch(key)


class DictImpl(fbase.StorageImplementation):
Expand Down Expand Up @@ -218,7 +216,7 @@ def dict(
:see: :func:`ring.dict` for non-asyncio version.
"""
return fbase.factory(
obj, key_prefix=key_prefix, ring_factory=ring_factory,
obj, key_prefix=key_prefix, ring_class_factory=ring_class_factory,
interface=interface, storage_implementation=storage_implementation,
miss_value=None, expire_default=expire, coder=coder,
ignorable_keys=ignorable_keys)
Expand Down Expand Up @@ -253,7 +251,7 @@ def aiomcache(
from ring._memcache import key_refactor

return fbase.factory(
client, key_prefix=key_prefix, ring_factory=ring_factory,
client, key_prefix=key_prefix, ring_class_factory=ring_class_factory,
interface=interface, storage_implementation=storage_implementation,
miss_value=None, expire_default=expire, coder=coder,
ignorable_keys=ignorable_keys,
Expand Down Expand Up @@ -282,7 +280,7 @@ def aioredis(
:see: :func:`ring.redis` for non-asyncio version.
"""
return fbase.factory(
pool, key_prefix=key_prefix, ring_factory=ring_factory,
pool, key_prefix=key_prefix, ring_class_factory=ring_class_factory,
interface=interface, storage_implementation=storage_implementation,
miss_value=None, expire_default=expire, coder=coder,
ignorable_keys=ignorable_keys)
Loading

0 comments on commit 21e3b39

Please sign in to comment.