Package: async-rediscache
Asynchronous Redis CacheThis package offers several data types to ease working with a Redis cache in an asynchronous workflow. The package is currently in development and it's not recommended to start using it in production at this point.InstallationPrerequisitesTo useasync-rediscache, make sure thatredisis installed and running on your system. Alternatively, you could usefakeredisas a back-end for testing purposes and local development.Install usingpipTo installasync-rediscacherun the following command:pipinstallasync-rediscacheAlternatively, to installasync-rediscachewithfakeredisrun:pipinstallasync-rediscache[fakeredis]Basic useCreating aRedisSessionTo use aRedisCache, you first have to create aRedisSessioninstance that manages the connection pool to Redis. You can create theRedisSessionat any point but make sure to call theconnectmethod from an asynchronous context (seethis explanationfor why).importasync_rediscacheasyncdefmain():session=async_rediscache.RedisSession()awaitsession.connect()# Do something interestingawaitsession.close()Creating aRedisSessionwith a network connectionasyncdefmain():connection={"address":"redis://127.0.0.1:6379"}async_rediscache.RedisSession(**connection)RedisCacheARedisCacheis the most basic data type provided byasync-rediscache. It works like a dictionary in that you can associate keys with values. To prevent key collisions, eachRedisCacheinstance should use a uniquenamespaceidentifier that will be prepended to the key when storing the pair to Redis.Creating aRedisCacheinstanceWhen creating aRedisCacheinstance, it's important to make sure that it has a uniquenamespace. This can be done directly by passing anamespacekeyword argument to the constructor:importasync_rediscachebirthday_cache=async_rediscache.RedisCache(namespace="birthday")Alternatively, if you assign a class attribute to aRedisCacheinstance, a namespace will be automatically generated using the name of the owner class and the name of attribute assigned to the cache:importasync_rediscacheclassChannel:topics=async_rediscache.RedisCache()# The namespace be set to `"Channel.topics"`Note: There is nothing preventing you from reusing the same namespace, although you should be aware this could lead to key collisions (i.e., one cache could interfere with the values another cache has stored).Using aRedisCacheinstanceUsing aRedisCacheis straightforward: Just call and await the methods you want to use and it should just work. There's no need to pass aRedisSessionaround as the session is fetched internally by theRedisCache. Obviously, one restriction is that you have to make sure that theRedisSessionis still open and connected when trying to use aRedisCache.Here are some usage examples:importasync_rediscacheasyncdefmain():session=async_rediscache.RedisSession()awaitsession.connect()cache=async_rediscache.RedisCache(namespace="python")# Simple key/value manipulationawaitcache.set("Guido","van Rossum")print(awaitcache.get("Guido"))# Would print `van Rossum`# A contains check works as wellprint(awaitcache.contains("Guido"))# Trueprint(awaitcache.contains("Kyle"))# False# You can iterate over all key, value pairs as well:item_view=awaitcache.items()forkey,valueinitem_view:print(key,value)# Other options:number_of_pairs=awaitcache.length()pairs_in_dict=awaitcache.to_dict()popped_item=awaitcache.pop("Raymond","Default value")awaitcache.update({"Brett":10,"Barry":False})awaitcache.delete("Barry")awaitcache.increment("Brett",1)# Increment Brett's int by 1awaitcache.clear()awaitsession.close()RedisQueueARedisQueueimplements the same interface as aqueue.SimpleQueueobject, except that all the methods are coroutines. Creating an instance works the same as with aRedisCache.