Caching

EsiPy is packaged with some default caching mechanism you may use.

 

DummyCache

This is a fake cache, only used to disable caching. This is used by default when cache is set to None in the EsiClient.

 

DictCache

This is the default EsiPy cache. It stores everything in memory dictionary and may then use many memory on your system.

 

FileCache

This cache use DiskCache to work, thus requires you to install diskcache using pip instal diskcache.
This cache will save data in files in a given directory, useful if you have many disc space but not much RAM.

When you instanciate the FileCache object, you’ll have to give the following parameters:

Parameter Type Description
path String The path where you want the files to be saved (diskCache will automatically create the directories if required)
settings kwargs The DiskCache parameters you may need. See Cache.__init__() parameters

Example:

# create the cache
from esipy.cache import FileCache
cache = FileCache(path="/tmp")

# and feed the client you create
client = EsiClient(cache=cache)

 

MemcachedCache

This cache use a memcache.Client object you give it as parameter to cache everything.
It requires you to have python-memcached installed.

# create the cache
import memcache
from esipy.cache import MemcachedCache
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
cache = MemcachedCache(memcache_client=mc)

# and feed the client you create
client = EsiClient(cache=cache)

 

RedisCache

This cache use a redis.Redis object you give it as parameter to cache everything.
It requires you to have python redis package installed.

# create the cache
import redis
from esipy.cache import RedisCache
redis_client = redis.Redis(host='localhost', port=6379, db=0)
cache = RedisCache(redis_client)

# and feed the client you create
client = EsiClient(cache=cache)

 

Your own custom cache for your custom needs

If you need a specific cache, because you already use your own, there’s a way to define a valid cache for EsiPy.

  1. First you need to inherit from esipy.cache.BaseCache and override the get, set and invalidate methods
  2. You should not delete outdated data from the cache within the cache process, else you will lose all the benefits from etags.

 

IMPORTANT: the caches keys used by EsiPy are tuples of frozendicts, which cannot be used everywhere. An existing _hash method allows you to get a md5 hash of the tuple, but you can define you own.

from esipy.cache import BaseCache

# this is the minimum required
class YourCache(BaseCache):

    def set(self, key, value, expire=300):
        # do something and store the value

    def get(self, key, default=None):
        # return the value or return default

    def invalidate(self, key):
        # invalidate the cache key