Skip to main content

Object Cacher

Simple cacher for your objects. Store copy of objects in memory (or pickle in file) and returns copy of that objects. Function arguments it is a cache key.

>>> from object_cacher import ObjectCacher
>>> @ObjectCacher(timeout=5)
... def test(*args):
...     print ('Real call')
...     return args
...
>>> test(1,2,3)
Real call
(1, 2, 3)
>>> test(1,2,3)
(1, 2, 3)
>>> test(1,2,3,4)
Real call
(1, 2, 3, 4)
>>> test(1,2,3,4)
... (1, 2, 3, 4)

Makes cache for results of hard functions or methods. For example you have remote RESTful api with a lot of dictionaries. You may cache it:

>>> from urllib import urlopen
>>> from object_cacher import ObjectCacher
>>> @ObjectCacher(timeout=60)
... def get_api():
...     print "This real call"
...     return urlopen('https://api.github.com/').read()
...
>>> get_api()
This real call
'{"current_user_url":"https://api.github.com/user", ...'
>>> get_api()
'{"current_user_url":"https://api.github.com/user", ...'

As result you made http request only once.

For methods you may use it like this:

>>> from urllib import urlopen
>>> from object_cacher import ObjectCacher
>>> class API(object):
...     @ObjectCacher(timeout=60, ignore_self=True)
...     def get_methods(self):
...         print "Real call"
...         return urlopen('https://api.github.com/').read()
...
>>> a = API()
>>> a.get_methods()
Real call
'{"current_user_url":"https://api.github.com/user", ...'
>>> b = API()
>>> b.get_methods()
'{"current_user_url":"https://api.github.com/user", ...'

If ignore_self parameter is set, cache will be shared by all instances. Otherwise cache for instances will be split.

Also you may use persistent cache. The “ObjectPersistentCacher” class-decorator makes file-based pickle-serialized cache storage. When you want to keep cache after rerun you must determine cache id:

>>> from urllib import urlopen
>>> from object_cacher import ObjectCacher
>>> class API(object):
...     @ObjectPersistentCacher(timeout=60, ignore_self=True, oid='com.github.api.listofmethods')
...     def get_methods(self):
...         print "Real call"
...         return urlopen('https://api.github.com/').read()
...
>>> a = API()
>>> a.get_methods()
Real call
'{"current_user_url":"https://api.github.com/user", ...'
>>> b = API()
>>> b.get_methods()
'{"current_user_url":"https://api.github.com/user", ...'

That is keep cache after rerun.

You may change cache dir for ObjectPersistentCacher via changing ‘CACHE_DIR’ class-property.

>>> ObjectPersistentCacher.CACHE_DIR = '/var/tmp/my_cache'

Installation

You may install from pypi

pip install object_cacher

or manual

python setup.py install

Metadata

Release files for object_cacher 0.3.1

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

Source distribution (sdist)

Source distribution for object_cacher 0.3.1
File Size Uploaded
object-cacher-0.3.1.tar.gz 3.8 kB Details

Release files / object-cacher-0.3.1.tar.gz

Download URL object-cacher-0.3.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
69996f88ac2bef118c1f2ac2ec0c8761caeec26762577eed915e478284f028c0
BLAKE2b-256 checksum
How to use checksums
843a3bbd47ab4a2062c721fc8b1ee09c60c11027f45d3f2653c59724ca3736b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.2.0

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

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