generated from guionardo/python-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from guionardo/fix/pipeline
Fix/pipeline
- Loading branch information
Showing
6 changed files
with
84 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: '3.8' | ||
services: | ||
cache: | ||
image: redis:6.2-alpine | ||
restart: always | ||
ports: | ||
- '6379:6379' | ||
command: redis-server --save 20 1 --loglevel warning | ||
volumes: | ||
- cache:/data | ||
volumes: | ||
cache: | ||
driver: local |
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
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,40 @@ | ||
"""Redis Cache""" | ||
import datetime | ||
import logging | ||
from typing import Union | ||
|
||
import redis.utils | ||
|
||
from .cache_protocol import Cache | ||
|
||
|
||
class RedisCache(Cache): | ||
"""Redis Cache""" | ||
|
||
def __init__(self): | ||
self.log = logging.getLogger(self.__class__.__name__) | ||
self.redis: redis.Redis = None | ||
|
||
def get(self, key: str) -> Union[str, None]: | ||
"""Return value or None if not found.""" | ||
value = self.redis.get(key) | ||
if isinstance(value, bytes): | ||
value = value.decode('utf-8') | ||
return value | ||
|
||
def set(self, key: str, value: str, | ||
ttl: datetime.timedelta = datetime.timedelta(seconds=0)) -> None: | ||
"""Set value with time to live""" | ||
if ttl.total_seconds() > 0: | ||
self.redis.set(key, value, ex=int(ttl.total_seconds())) | ||
elif ttl.total_seconds() == 0: | ||
self.redis.set(key, value) | ||
|
||
def parse(self, connection_string: str) -> 'Cache': | ||
"""Parse connection string and return instance of Cache if valid.""" | ||
if not connection_string.startswith('redis://'): | ||
return None | ||
|
||
self.redis = redis.utils.from_url(connection_string) | ||
self.log.info('Initialized') | ||
return self |
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,17 @@ | ||
"""Mocking redis""" | ||
|
||
|
||
class FakeRedis(): | ||
"""Fake redis class""" | ||
|
||
def get(self, key: str): | ||
return {'test_key': b'test_value', | ||
'test_key2': None}.get(key) | ||
|
||
def set(self, *args, **kwargs): | ||
... | ||
|
||
|
||
def fake_from_url(*args, **kwargs): | ||
"""Fake from_url function""" | ||
return FakeRedis() |
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