Created
January 22, 2011 02:15
-
-
Save leandrosilva/790787 to your computer and use it in GitHub Desktop.
Just a dummy abstraction for a Redis-based cache (in Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Cachedis | |
| ~~~~~ | |
| Just a dummy abstraction for a Redis-based cache. | |
| https://gist.github.com/790787 | |
| :copyright: (c) 2010 by Leandro Silva (CodeZone). | |
| :license: Just FREE. | |
| """ | |
| from .cachedis import Cachedis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from redis import Redis | |
| class Cachedis(object): | |
| """Cachedis is a dummy abstraction for a Redis-based cache (with more comments that code). | |
| It's really simple to use. For example: | |
| r = Cachedis() | |
| r.put("a", 1) | |
| r.get("a") # returns '1' | |
| r.put("b", 2, 10) # 10 seconds | |
| r.get("b") # returns '2' | |
| r.get("b") # after 10 seconds, it'll returns none | |
| It also requires that the redis egg is installed. To get this: | |
| $ easy_install redis | |
| """ | |
| def __init__(self, host="localhost", port=6379, db=0): | |
| """Initializes a new Cachedis object. | |
| On create a new instance of Cachedis, this method also connects to Redis server. | |
| Args: | |
| host: The Redis host address. Default is "localhost". | |
| port: The Redis port. Default is 6379. | |
| db: The Redis database. Default is 0. | |
| """ | |
| self.__redis = Redis(host, port, db) | |
| def put(self, key, value, expiration_time=3600): | |
| """Puts an object to the cache. | |
| Args: | |
| key: The key for the cached object. | |
| value: The object been cached. | |
| expiration_time: The time to wait until expires and removes the key/value from | |
| the cache. Default is 3600 seconds. | |
| Raises: | |
| redis.exceptions.ConnectionError: An error if there is some problem on the connection | |
| with Redis server. | |
| """ | |
| self.__redis.setex(key, value, expiration_time) | |
| def get(self, key): | |
| """Gets a object from the cache. | |
| Args: | |
| key: The key for the cached object that will to be retrieved. | |
| Raises: | |
| redis.exceptions.ConnectionError: An error if there is some problem on the connection | |
| with Redis server. | |
| """ | |
| return self.__redis.get(key) | |
| def remove(self, key): | |
| """Removes a object from the cache. | |
| Args: | |
| key: The key for the cached object that will to be removed. | |
| Raises: | |
| redis.exceptions.ConnectionError: An error if there is some problem on the connection | |
| with Redis server. | |
| """ | |
| return self.__redis.lpop(key) | |
| def clear(self): | |
| """Deletes all objects in the cache. | |
| Raises: | |
| redis.exceptions.ConnectionError: An error if there is some problem on the connection | |
| with Redis server. | |
| """ | |
| self.__redis.flushdb() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment