Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ring.coder # noqa
from ring.__version__ import __version__ # noqa
from ring.func import (
lru, dict, shelve, disk, memcache, redis, redis_hash)
lru, dict, shelve, disk, memcache, redis, redis_hash, dogpile)
try:
import asyncio
from ring.func.asyncio import aiomcache, aioredis, aioredis_hash
Expand All @@ -22,4 +22,4 @@

__all__ = (
'lru', 'dict', 'shelve', 'memcache', 'redis', 'redis_hash', 'disk',
'aiomcache', 'aioredis', 'aioredis_hash')
'aiomcache', 'aioredis', 'aioredis_hash', 'dogpile')
4 changes: 3 additions & 1 deletion ring/func/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
else:
from ring.func import asyncio

from .sync import dogpile

__all__ = (
'lru', 'dict', 'memcache', 'redis', 'redis_hash', 'shelve', 'disk')
'lru', 'dict', 'memcache', 'redis', 'redis_hash', 'shelve', 'disk',
'dogpile')


if _has_asyncio:
Expand Down
49 changes: 49 additions & 0 deletions ring/func/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ def delete_value(self, key):
self.backend.delete(key)


class DogpileStorage(fbase.CommonMixinStorage, fbase.StorageMixin):

def get_value(self, key):
import dogpile.cache
expire = self.rope.expire_default # FIXME
value = self.backend.get(key, expiration_time=expire)
if value == dogpile.cache.api.NO_VALUE:
raise fbase.NotFound
return value

def set_value(self, key, value, expire):
self.backend.set(key, value)

def delete_value(self, key):
self.backend.delete(key)


def lru(
lru=None, key_prefix=None, expire=None, coder=None,
user_interface=CacheUserInterface, storage_class=LruStorage,
Expand Down Expand Up @@ -672,6 +689,38 @@ def diskcache(
**kwargs)


def dogpile(
region, key_prefix=None, coder=None,
user_interface=CacheUserInterface, storage_class=DogpileStorage,
**kwargs):
"""dogpile_ interface.

.. _dogpile: https://dogpilecache.sqlalchemy.org/

:param dogpile.cache.region.CacheRegion region: dogpile region object.

>>> region = dogpile.cache.make_region().configure(
'dogpile.cache.pylibmc',
expiration_time = 3600,
arguments = {
'url': ["127.0.0.1"],
}
)
>>> @ring.dogpile(region, ...)
... ...

:see: :func:`ring.func.sync.CacheUserInterface` for sub-functions.
"""
expire = None # set by dogpile region

return fbase.factory(
region, key_prefix=key_prefix, on_manufactured=None,
user_interface=user_interface, storage_class=storage_class,
miss_value=None, expire_default=expire, coder=coder,
key_refactor=region.key_mangler,
**kwargs)


def arcus(
client, key_prefix=None, expire=0, coder=None,
default_action='get_or_update',
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def get_version():
'redis', 'requests',
'diskcache',
'django',
'dogpile.cache',
]
docs_require = [
'sphinx',
Expand Down
26 changes: 25 additions & 1 deletion tests/test_func_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import memcache
import redis
import diskcache
from dogpile.cache import make_region

from ring._memcache import key_refactor
from ring.func.lru_cache import LruCache

import pytest
Expand Down Expand Up @@ -76,6 +79,26 @@ def storage_diskcache(request):
return client


@pytest.fixture(scope='session', params=[
make_region(key_mangler=key_refactor).configure(
'dogpile.cache.pylibmc',
expiration_time=10,
arguments={
'url': ["127.0.0.1"],
}
)
])
def storage_dogpile(request):
client = request.param
client.ring = ring.dogpile
client.is_binary = False
client.has_has = False
client.has_touch = False
client.has_expire = False
assert(client.is_configured)
return client


@pytest.fixture(scope='session', ids=['python-memcached', 'pymemcache', 'pylibmc'], params=[
# client, binary, has_touch
(pythonmemcache_client, False, sys.version_info[0] == 2),
Expand Down Expand Up @@ -114,6 +137,7 @@ def redis_client(request):
lazy_fixture('memcache_client'),
lazy_fixture('redis_client'),
lazy_fixture('storage_diskcache'),
lazy_fixture('storage_dogpile'),
])
def storage(request):
return request.param
Expand Down Expand Up @@ -344,7 +368,7 @@ def test_common_value(storage):

base = [b'a']

@storage.ring(storage, key_prefix=str(storage), **options)
@storage.ring(storage, key_prefix=str(storage) + '::', **options)
def ff():
base[0] += b'b'
return base[0]
Expand Down