def test_set_item_tags(self):
        """ Setting item tags """

        key1 = 'somekey'
        data1 = 'some date to put to cache'
        key2 = 'other'
        data2 = 'some other data'

        redis = Redis('test')
        redis.set(key1, data1)
        redis.set(key2, data2)

        tags = ['tag1', 'tag2']
        redis.set_tags(key1, tags)
        redis.set_tags(key2, tags)

        set1 = redis.get_tagged_items('tag1')
        set2 = redis.get_tagged_items('tag2')

        # assert tags created and contain item
        self.assertTrue(type(set1) is set)
        self.assertTrue(redis.get_full_item_key(key1) in set1)
        self.assertTrue(redis.get_full_item_key(key2) in set1)

        self.assertTrue(type(set2) is set)
        self.assertTrue(redis.get_full_item_key(key1) in set2)
        self.assertTrue(redis.get_full_item_key(key2) in set2)
 def test_return_key_if_already_full(self):
     """ Do not create full key from full key, just return """
     namespace = 'testing'
     key = 'some-key'
     redis = Redis(namespace)
     full_key = redis.get_full_item_key(key)
     self.assertEqual(full_key, redis.get_full_item_key(full_key))
 def test_get_full_key(self):
     """ Generating full namespaced key from short cache key"""
     namespace = 'testing'
     separator = '::'
     key = 'some-key'
     redis = Redis(namespace)
     full_key = redis.get_full_item_key(key)
     self.assertEqual(namespace + separator + key, full_key)
 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'))
 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'))
 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'))
    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'))
 def test_can_set_with_tags(self):
     """ Doing set item with tags """
     key = 'somekey'
     data = 'some date to put to cache'
     tags = ['tag']
     redis = Redis('test')
     redis.set(key, data, tags=tags)
     item_tags = redis.get_item_tags(key)
     tagged_items = redis.get_tagged_items('tag')
     self.assertIn('tag', item_tags)
     self.assertIn(redis.get_full_item_key(key), tagged_items)
    def test_can_detect_full_keys(self):
        """ Detecting full item key """

        namespace = 'testing'
        key = 'some-key'
        redis = Redis(namespace)

        full_key = redis.get_full_item_key(key)
        not_full_key = 'x' + full_key
        bogus = 'bad'

        self.assertTrue(redis.is_full_item_key(full_key))
        self.assertFalse(redis.is_full_item_key(not_full_key))
        self.assertFalse(redis.is_full_item_key(bogus))
Beispiel #10
0
 def test_collect_garbage_initial(self):
     """ Garbage collect does nothing on first run """
     redis = Redis('test')
     gc_key = redis.get_full_item_key('__gc')
     self.assertIsNotNone(redis.get(gc_key))