class Cache:
    """ Cache class
    """
    def __init__(self):
        """ Store an instance of the Redis client as a private variable
            named _redis
        """
        self._redis = Redis()
        self._redis.flushdb()

    @call_history
    @count_calls
    def store(self, data: Union[str, bytes, int, float]) -> str:
        """ Takes a data argument and returns a string
        """
        key = str(uuid4())
        self._redis.set(key, data)
        return key

    def get(self, key: str, fn: Optional[Callable] = None) ->\
            Union[str, bytes, int, float]:
        """ Take a key string argument and an optional Callable argument
            named fn
        """
        if key:
            result = self._redis.get(key)
            if fn:
                return fn(result)
            else:
                return result

    def get_str(self, data: bytes) -> str:
        """ Will automatically parametrize Cache.get with the correct
            conversion function
        """
        return data.decode('utf-8')

    def get_int(self, data: bytes) -> int:
        """ Will automatically parametrize Cache.get with the correct
            conversion function
        """
        byte_order = sys.byteorder
        return int.from_bytes(data, byte_order)
Пример #2
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    logging.getLogger("c2corg_api").setLevel(logging.INFO)

    redis_url = "{0}?db={1}".format(settings["redis.url"], settings["redis.db_cache"])
    log.info("Cache Redis: {0}".format(redis_url))

    # we don't really need a connection pool here, but the `from_url`
    # function is convenient
    redis_pool = ConnectionPool.from_url(redis_url, max_connections=1)

    # remove all keys from the database
    r = Redis(connection_pool=redis_pool)
    r.flushdb()
    log.info("Flushed cache")
Пример #3
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    logging.getLogger('c2corg_ui').setLevel(logging.INFO)

    log.info('Cache Redis: {0}'.format(settings['redis.url']))

    # we don't really need a connection pool here, but the `from_url`
    # function is convenient
    redis_pool = ConnectionPool.from_url(
        settings['redis.url'], max_connections=1)

    # remove all keys from the database
    r = Redis(connection_pool=redis_pool)
    r.flushdb()
    log.info('Flushed cache')
Пример #4
0
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    logging.getLogger('c2corg_ui').setLevel(logging.INFO)

    log.info('Cache Redis: {0}'.format(settings['redis.url']))

    # we don't really need a connection pool here, but the `from_url`
    # function is convenient
    redis_pool = ConnectionPool.from_url(
        settings['redis.url'], max_connections=1)

    # remove all keys from the database
    r = Redis(connection_pool=redis_pool)
    r.flushdb()
    log.info('Flushed cache')
Пример #5
0
class Cache:
    """Redis Class"""
    def __init__(self):
        """constructor"""
        self._redis = Redis()
        self._redis.flushdb()

    @call_history
    @count_calls
    def store(self, data: Union[str, bytes, int, float]) -> str:
        """returns a string"""
        key = str(uuid.uuid4())
        self._redis.set(key, data)
        return key

    def get(self, key: str, fn: Optional[Callable] = None) ->\
            Union[str, bytes, int, float]:
        """get method"""
        if key:
            result = self._redis.get(key)
            return fn(result) if fn else result
class Cache:
    """Cache class"""
    def __init__(self):
        """constructor"""
        self._redis = Redis()
        self._redis.flushdb()

    @call_history
    @count_calls
    def store(self, data: Union[str, bytes, int, float]) -> str:
        """Store data in cache"""
        key = str(uuid.uuid4())
        self._redis.set(key, data)
        return key

    def get(self, key: str, fn: Optional[Callable] = None) ->\
            Union[str, bytes, int, float]:
        """get data from fuction or cache"""
        if key:
            result = self._redis.get(key)
            if fn:
                return fn(result)
            else:
                return result
Пример #7
0
class RUnitTest(BaseTest):
    def setUp(self):
        self.redis = Redis()
        self.redis.flushdb()
        self.model = TestModel(self.redis)

    def test_init(self):
        self.assertTrue(isinstance(self.model.cursor, Cursor))

    def test_incr(self):
        global incr_value

        model = ModelWithIncr(redis=self.redis)

        self.assertEqual(model.incr_field.get(), 0)
        model.incr_field += 1
        self.assertEqual(model.incr_field.get(), 1)

        incr_value = 13

        model.incr_field += 11
        self.assertEqual(model.incr_field.get(), 1 + 13 + 11)

        model.incr_field.set(10)

        self.assertEqual(model.incr_field.get(), 10)

    def test_simple(self):
        self.assertEqual(self.model.id.get(), None)
        self.assertEqual(self.model.name.get(), None)

        self.model.id.set(1)
        self.assertEqual(self.model.id.get(), 1)
        self.model.name.set('test_name')
        self.model = TestModel(redis=self.redis)

        self.assertEqual(self.model.id.get(), 1)
        self.assertEqual(self.model.name.get(), 'test_name')
        self.assertEqual(self.model.data(), {'id': 1, 'name': 'test_name'})

    def test_data(self):
        self.model.id.set(1)
        self.model.name.set('test_name')

        self.assertEqual(self.model.id.get(), 1)
        self.assertEqual(self.model.name.get(), 'test_name')

        self.assertEqual(self.model.data(), {'id': 1, 'name': 'test_name'})

    def test_nested_model(self):
        model = NestedModel(redis=self.redis)
        self.assertEqual(len(model.fields()), 3)
        model.nested.store.set(1)
        self.assertEqual(model.nested.store.get(), 1)
        self.assertDictEqual(model.data(), {
            'nested': {
                'store': 1
            },
            'id': None,
            'name': None
        })

    def test_cross_model(self):
        one, two = (NestedModel(prefix=1, redis=self.redis),
                    NestedModel(prefix=2, redis=self.redis))
        self.assertEqual(one.cursor.key, '1')
        self.assertEqual(two.cursor.key, '2')

        one.id.set(1)
        two.id.set(2)
        self.assertEqual(one.id.get(), 1)
        self.assertEqual(two.id.get(), 2)

        self.assertEqual(one.nested.cursor.key, '1:nested')
        self.assertEqual(two.nested.cursor.key, '2:nested')
        one.nested.store.set(1)
        two.nested.store.set(2)
        self.assertNotEqual(one.nested.store, two.nested.store)
        self.assertEqual(one.nested.store.get(), 1)
        self.assertEqual(two.nested.store.get(), 2)

    def test_defaults(self):
        TestModel.defaults = {'id': 334, 'name': 'HELLO'}

        self.assertEqual(self.model.id.get(), 334)
        self.assertEqual(self.model.name.get(), 'HELLO')
        TestModel.defaults = False

    def test_session_remove(self):
        session = self.model._session = RSession()
        self.model.remove()
        self.eq(session.changes(), {self.model.prefix: None})
Пример #8
0
class BaseTest(ZTest):
    def setUp(self):
        self.redis = Redis()
        self.redis.flushdb()
        self.model = TModel(redis=self.redis)
Пример #9
0
class BaseTest(ZTest):
    def setUp(self):
        self.redis = Redis()
        self.redis.flushdb()
        self.model = TModel(redis=self.redis)
Пример #10
0
class RUnitTest(BaseTest):
    def setUp(self):
        self.redis = Redis()
        self.redis.flushdb()
        self.model = TestModel(self.redis)

    def test_init(self):
        self.assertTrue(isinstance(self.model.cursor, Cursor))

    def test_incr(self):
        global incr_value

        model = ModelWithIncr(redis=self.redis)

        self.assertEqual(model.incr_field.get(), 0)
        model.incr_field += 1
        self.assertEqual(model.incr_field.get(), 1)

        incr_value = 13

        model.incr_field += 11
        self.assertEqual(model.incr_field.get(), 1 + 13 + 11)

        model.incr_field.set(10)

        self.assertEqual(model.incr_field.get(), 10)

    def test_simple(self):
        self.assertEqual(self.model.id.get(), None)
        self.assertEqual(self.model.name.get(), None)

        self.model.id.set(1)
        self.assertEqual(self.model.id.get(), 1)
        self.model.name.set('test_name')
        self.model = TestModel(redis=self.redis)

        self.assertEqual(self.model.id.get(), 1)
        self.assertEqual(self.model.name.get(), 'test_name')
        self.assertEqual(self.model.data(), {'id': 1, 'name': 'test_name'})

    def test_data(self):
        self.model.id.set(1)
        self.model.name.set('test_name')

        self.assertEqual(self.model.id.get(), 1)
        self.assertEqual(self.model.name.get(), 'test_name')

        self.assertEqual(self.model.data(), {'id': 1, 'name': 'test_name'})

    def test_nested_model(self):
        model = NestedModel(redis=self.redis)
        self.assertEqual(len(model.fields()), 3)
        model.nested.store.set(1)
        self.assertEqual(model.nested.store.get(), 1)
        self.assertDictEqual(model.data(), {'nested': {'store': 1},
                                            'id': None, 'name': None})

    def test_cross_model(self):
        one, two = (NestedModel(prefix=1, redis=self.redis),
                    NestedModel(prefix=2, redis=self.redis))
        self.assertEqual(one.cursor.key, '1')
        self.assertEqual(two.cursor.key, '2')

        one.id.set(1)
        two.id.set(2)
        self.assertEqual(one.id.get(), 1)
        self.assertEqual(two.id.get(), 2)

        self.assertEqual(one.nested.cursor.key, '1:nested')
        self.assertEqual(two.nested.cursor.key, '2:nested')
        one.nested.store.set(1)
        two.nested.store.set(2)
        self.assertNotEqual(one.nested.store, two.nested.store)
        self.assertEqual(one.nested.store.get(), 1)
        self.assertEqual(two.nested.store.get(), 2)

    def test_defaults(self):
        TestModel.defaults = {'id': 334, 'name': 'HELLO'}

        self.assertEqual(self.model.id.get(), 334)
        self.assertEqual(self.model.name.get(), 'HELLO')
        TestModel.defaults = False

    def test_session_remove(self):
        session = self.model._session = RSession()
        self.model.remove()
        self.eq(session.changes(), {self.model.prefix: None})