Skip to content

Commit

Permalink
Merge pull request #2 from guionardo/fix/pipeline
Browse files Browse the repository at this point in the history
Fix/pipeline
  • Loading branch information
guionardo authored Jun 27, 2022
2 parents 5a2d969 + 9e8f4dd commit b4e312c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docker-compose.yaml
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
2 changes: 1 addition & 1 deletion gs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Package data"""
__version__ = '0.1.4'
__version__ = '0.1.5'
__tool_name__ = 'py-gstools'
__description__ = 'Tool classes and functions for Guiosoft projects'
__author__ = 'Guionardo Furlan'
Expand Down
5 changes: 3 additions & 2 deletions gs/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Generic cache module."""

from .cache_protocol import Cache
from .file_cache import FileCache
from .memory_cache import MemoryCache
from .cache_protocol import Cache
from .redis_cache import RedisCache


def get_cache(connection_string: str) -> Cache:
"""Get cache instance from connection string."""
for cache in [MemoryCache, FileCache]:
for cache in [MemoryCache, FileCache, RedisCache]:
instance = cache().parse(connection_string)
if instance:
return instance
Expand Down
40 changes: 40 additions & 0 deletions gs/cache/redis_cache.py
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
17 changes: 17 additions & 0 deletions tests/cache/mock_cache_redis.py
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()
11 changes: 10 additions & 1 deletion tests/cache/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import datetime
import tempfile
import unittest
from unittest.mock import patch

from gs.cache import Cache, FileCache, MemoryCache, get_cache
from gs.cache import Cache, FileCache, MemoryCache, RedisCache, get_cache

from .mock_cache_redis import fake_from_url


class TestCache(unittest.TestCase):
Expand All @@ -25,6 +28,12 @@ def test_file_cache(self):
cache = get_cache(f'path://{tmpdir}')
self._test_cache(cache, FileCache)

@patch('redis.utils.from_url', fake_from_url)
def test_redis_cache(self):
"""Test redis cache"""
cache = get_cache('redis://localhost:6379/0')
self._test_cache(cache, RedisCache)

def _test_cache(self, cache: Cache, class_type):
self.assertIsInstance(cache, class_type)
cache.set('test_key', 'test_value')
Expand Down

0 comments on commit b4e312c

Please sign in to comment.