-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9d846a
commit b8bd3ee
Showing
3 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import redis | ||
|
||
|
||
class Cache: | ||
def __init__(self, type="in_memory", **kwargs): | ||
self.type = type | ||
if type == "redis": | ||
self.cache = redis.Redis(host=kwargs["host"], port=kwargs["port"], db=0) | ||
else: | ||
self.cache = {} | ||
|
||
self.func_map = { | ||
"redis": {"set": self._set_redis_key, "get": self._get_redis_key}, | ||
"in_memory": { | ||
"set": self._set_in_memory_key, | ||
"get": self._get_in_memory_key, | ||
}, | ||
} | ||
|
||
def set(self, key, value): | ||
self.func_map[self.type]["set"](key, value) | ||
|
||
def get(self, key): | ||
return self.func_map[self.type]["get"](key) | ||
|
||
def _set_redis_key(self, key: str, value): | ||
self.cache.set(key, value) | ||
|
||
def _set_in_memory_key(self, key, value): | ||
self.cache[key] = value | ||
|
||
def _get_redis_key(self, key): | ||
return self.cache.get(key) | ||
|
||
def _get_in_memory_key(self, key): | ||
return self.cache.get(key) |
This file contains 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