Example #1
0
 def test_can_set(self):
     """ Simple item set """
     key = 'somekey'
     data = 'some date to put to cache'
     redis = Redis('test')
     redis.set(key, data)
     full_key = redis.get_full_item_key(key)
     self.assertIsNotNone(redis.get_redis().hget(full_key, 'data'))
Example #2
0
 def test_can_set_with_expiration(self):
     """ Doing set item with expiration """
     key = 'somekey'
     data = 'some date to put to cache'
     expire = '+1second'
     redis = Redis('test')
     redis.set(key, data, expires_at=expire)
     full_key = redis.get_full_item_key(key)
     time.sleep(1.1)
     self.assertIsNone(redis.get_redis().hget(full_key, 'data'))
Example #3
0
 def test_can_set_with_ttl(self):
     """ Doing set item with custom ttl"""
     ttl = 1
     key = 'somekey'
     data = 'some date to put to cache'
     redis = Redis('test')
     redis.set(key, data, ttl=ttl)
     full_key = redis.get_full_item_key(key)
     time.sleep(1)
     self.assertIsNone(redis.get_redis().hget(full_key, 'data'))
Example #4
0
    def test_can_optimize(self):
        """ Performing storage optimization """

        redis = Redis('test')
        data = 'this data will be used for everything'

        key1 = 'item1'
        tags1 = ['tag1', 'tag2', 'tag3']
        key2 = 'item2'
        tags2 = ['tag1', 'tag2', 'tag3']
        key3 = 'item3'
        tags3 = ['tag3', 'tag4', 'tag5']
        key4 = 'item4'
        tags4 = ['tag3', 'tag4', 'tag5']

        redis.set(key1, data, ttl=1, tags=tags1)
        redis.set(key2, data, ttl=1, tags=tags2)

        redis.set(key3, data, tags=tags3)
        redis.set(key4, data, tags=tags4)

        redis.get_redis().delete(redis.get_tag_set_key('tag4'))
        redis.get_redis().delete(redis.get_tag_set_key('tag5'))

        time.sleep(1.1)
        redis.optimize()

        # missing items should be removed from tags
        self.assertNotIn(redis.get_full_item_key(key1),
                         redis.get_tagged_items('tag3'))
        self.assertNotIn(redis.get_full_item_key(key2),
                         redis.get_tagged_items('tag3'))

        # empty tags should be removed
        self.assertFalse(redis.get_tagged_items('tag1'))
        self.assertFalse(redis.get_tagged_items('tag2'))

        # missing tags should be removed from items
        self.assertNotIn('tag4', redis.get_item_tags('item3'))
        self.assertNotIn('tag4', redis.get_item_tags('item4'))
        self.assertNotIn('tag5', redis.get_item_tags('item3'))
        self.assertNotIn('tag5', redis.get_item_tags('item4'))
Example #5
0
 def test_can_put_to_redis(self):
     """ Can do basic put to storage """
     redis = Redis('testing')
     redis.get_redis().set('foo', 'bar')
     result = redis.get_redis().get('foo')
     self.assertEqual('bar', result)
Example #6
0
 def test_get_redis(self):
     """ Getting redis connection """
     adapter = Redis('testing')
     redis = adapter.get_redis()
     self.assertIsInstance(redis, StrictRedis)
Example #7
0
 def tearDown(self):
     redis = Redis('test')
     redis.get_redis().flushdb()
     TestCase.tearDown(self)