Skip to content

Commit

Permalink
fix(javatarz#12): addition of redis in pipfile, cache duration in arg…
Browse files Browse the repository at this point in the history
…s instead of having constant
  • Loading branch information
Sathia committed Oct 19, 2019
1 parent c8f7173 commit 84d01bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ requests = "*"
flask = "*"
flask-jsonpify = "*"
flask-restful = "*"
redis = "*"

[requires]
python_version = "3.7"
16 changes: 12 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions ghcl/cache.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import redis


def singleton(class_):
instances = {}

def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance


@singleton
class Cache:
CACHE_DURATION_IN_SECONDS = 60

def __init__(self):
self.redis = redis.Redis()
def __init__(self, cache_duration=60):
self.redis = redis.Redis()
self.cache_duration=cache_duration

def exists(self, cache_key) -> bool:
return self.redis.exists(cache_key)
def exists(self, cache_key) -> bool:
return self.redis.exists(cache_key)

def set(self, cache_key, value) -> bool:
return self.redis.set(cache_key, value, self.CACHE_DURATION_IN_SECONDS)
def set(self, cache_key, value) -> bool:
return self.redis.set(cache_key, value, self.cache_duration)

def fetch(self, cache_key) -> str:
return self.redis.get(cache_key)
def fetch(self, cache_key) -> str:
return self.redis.get(cache_key)

0 comments on commit 84d01bc

Please sign in to comment.