Пример #1
0
def query_memcache(key):
    """Check if cached permissions are available

  Args:
      key (string): key of the stored permissions
  Returns:
      cache (memcache_client): memcache client or None if caching
                               is not available
      permissions_cache (dict): dict with all permissions or None if there
                                was a cache miss
  """
    if not getattr(settings, 'MEMCACHE_MECHANISM', False):
        return None, None

    cache = cache_utils.get_cache_manager().cache_object.memcache_client
    cached_keys_set = cache.get('permissions:list') or set()
    if key not in cached_keys_set:
        # We set the permissions:list variable so that we are able to batch
        # remove all permissions related keys from memcache
        cached_keys_set.add(key)
        cache.set('permissions:list', cached_keys_set,
                  PERMISSION_CACHE_TIMEOUT)
        return cache, None

    return cache, memcache.blob_get(cache, key, namespace=PERMISSION_NAMESPACE)
Пример #2
0
 def test_big_value_saving(self):
     """Test storing in memcache huge chunk of data."""
     # Create a structure with a lot of data. It should take more than 1 Mb.
     data = {"a": {"b": ["test" * 10 for _ in range(10**6)]}}
     self.assertTrue(memcache.blob_set(self.memcache_client, "data", data))
     self.assertGreater(self.memcache_client.get_stats().get("bytes"),
                        memcache.MEMCACHE_MAX_ITEM_SIZE)
     self.assertEqual(data, memcache.blob_get(self.memcache_client, "data"))
Пример #3
0
 def test_blob_set_in_namespace(self):
   """Test blob set/get operation in namespace"""
   # set cache which will be split to 3 chunks (5+5+1 bytes)
   memcache.blob_set(self.memcache_client, "a", "01234567890", namespace="n")
   result = memcache.blob_get(self.memcache_client, "a", namespace="n")
   self.assertEqual(result, "01234567890")
   self.assertEqual(self.memcache_client.get("a", namespace="n"),
                    ["a:0", "a:5", "a:10"])
   self.assertEqual(self.memcache_client.get("a:0", namespace="n"), "01234")
Пример #4
0
  def test_update(self):
    """Test blob set/set operation"""
    # set cache which will be split to 3 chunks (5+5+1 bytes)
    memcache.blob_set(self.memcache_client, "a", "01234567890")
    # set cache with 2 chunks (5+2 bytes)
    memcache.blob_set(self.memcache_client, "a", "abcdefg")

    result = memcache.blob_get(self.memcache_client, "a")
    self.assertEqual(result, "abcdefg")
Пример #5
0
  def test_blob_set_get(self):
    """Test blob set/get operation"""
    # set cache which will be split to 3 chunks (5+5+1 bytes)
    data = "01234567890"

    memcache.blob_set(self.memcache_client, "a", data)
    result = memcache.blob_get(self.memcache_client, "a")

    self.assertEqual(result, data)
Пример #6
0
  def test_update_namespace(self):
    """Test blob_set() removes chunks from correct namespace"""
    # set cache which will be split to 3 chunks (5+5+1 bytes)
    memcache.blob_set(self.memcache_client, "a", "01234567890", namespace="n")
    # set cache with 2 chunks (5+2 bytes), which must remove previous chunks
    memcache.blob_set(self.memcache_client, "a", "abcdefg", namespace="n")

    result = memcache.blob_get(self.memcache_client, "a:10", namespace="n")
    # Ensure that chunk are removed from correct namespace
    self.assertIsNone(result)
Пример #7
0
 def test_big_value_saving(self):
   """Test storing in memcache huge chunk of data."""
   # Create a structure with a lot of data. It should take more than 1 Mb.
   data = {"a": {"b": ["test" * 10 for _ in range(10 ** 6)]}}
   self.assertTrue(memcache.blob_set(self.memcache_client, "data", data))
   self.assertGreater(
       self.memcache_client.get_stats().get("bytes"),
       memcache.MEMCACHE_MAX_ITEM_SIZE
   )
   self.assertEqual(
       data,
       memcache.blob_get(self.memcache_client, "data")
   )
Пример #8
0
def query_memcache(cache, key):
  """Get cached permissions from memcache is available

  Args:
      cache (memcache client): mecahce client
      key (string): key of the stored permissions
  Returns:
      permissions_cache (dict): dict with all permissions or None if there
                                was a cache miss
  """

  cached_keys_set = cache.get('permissions:list') or set()
  if key not in cached_keys_set:
    # We set the permissions:list variable so that we are able to batch
    # remove all permissions related keys from memcache
    cached_keys_set.add(key)
    cache.set('permissions:list', cached_keys_set, PERMISSION_CACHE_TIMEOUT)
    return None

  return memcache.blob_get(cache, key)
Пример #9
0
def query_memcache(cache, key):
  """Get cached permissions from memcache is available

  Args:
      cache (memcache client): mecahce client
      key (string): key of the stored permissions
  Returns:
      permissions_cache (dict): dict with all permissions or None if there
                                was a cache miss
  """

  cached_keys_set = cache.get('permissions:list') or set()
  if key not in cached_keys_set:
    # We set the permissions:list variable so that we are able to batch
    # remove all permissions related keys from memcache
    cached_keys_set.add(key)
    cache.set('permissions:list', cached_keys_set, PERMISSION_CACHE_TIMEOUT)
    return None

  return memcache.blob_get(cache, key)