Skip to main content
Cache object
~~~~~~~~~~~~

Cache object is general purpose object-property interface.

Example
-------
There is some pre-defined common types.

in-memory, memcache and redis backends are included. And its common general interfaces too.

>>> from cacheobj.redis import LocalRedisObject
>>> class UserCache(LocalRedisObject):
... _properties = ['username', 'name']
...
>>> user = UserCache(id=10)
>>> print user.username
None
>>> user.username = 'username'
>>> user.name = 'Real Name'
>>> print user.username
username
>>> user10 = UserCache(id=10)
>>> print user10.username
username

Cache is stored with class name and given id.

>>> print user.get('username', use_cache=True)
username

If you don't want to hit backend again, there is use_cache option for local memory cache.

For non-local cache backend, try a easy generator.

>>> from cacheobj.redis import get_redis_object
>>> import redis
>>> MyRedisObject = get_redis_object(redis.ConnectionPool()) # any connection pool
>>> class MyUserCache(MyRedisObject):
... pass
...

Custom Backend
--------------

Upper examples are shortcut for basic configuration.

>>> from cacheobj import SimpleCacheObject
>>> from cacheobj.backends.memcache import MemcacheBackend
>>> def get_backend():
... return MemcacheBackend(['127.0.0.1:11211'])
...
>>> class UserCache(SimpleCacheObject):
... _backend_generator = staticmethod(get_backend)
... _properties = ['username', 'name']
...

This object works as upper UserCache object.
You can put custom backend with this.

See source code to write a backend. It is just a few lines.

Composite Example
-----------------

You can composite multiple backends for an object.

>>> from cacheobj import CacheObject
>>> from cacheobj.backends.inmemory import InMemoryBackend
>>> from cacheobj.backends.memcache import MemcacheBackend
>>> from cacheobj.backends.redis import RedisBackend
>>>
>>> memory = InMemoryBackend()
>>> memcache = MemcacheBackend()
>>> redis = RedisBackend()
>>>
>>> class CompositeCache(CacheObject):
... _backends = {
... memory: ['mem1', 'mem2'],
... memcache: ['mc1', 'mc2'],
... redis: ['redis1', 'redis2'],
... }
...
>>> c = CompositeCache()
>>> c.mem1 # with backend memory
>>> c.mc1 # with backend memcache
>>> c.redis1 # with backend redis

Release files for cacheobj 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cacheobj 0.5
File Size Uploaded
cacheobj-0.5.tar.gz 4.2 kB Details

Release files / cacheobj-0.5.tar.gz

Download URL cacheobj-0.5.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6a8cef91833bdf7d9594c27a215634a2fae827b2efc6d6cc1dd320a8f3a481b1
BLAKE2b-256 checksum
How to use checksums
3ac67394d74482a13c9855c4d4ef4854588b63c5f3e15d24faa07746341041b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.9

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

0.6.1

1 release file

0.6

1 release file

0.5.1

1 release file

This release

0.5 This release

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page