Example #1
0
def clear_memcache():
    """Flush memcahce if available"""

    if not has_memcache():
        return

    get_cache_manager().clean()
Example #2
0
def clear_memcache():
  """Flush memcahce if available"""

  if not has_memcache():
    return

  get_cache_manager().clean()
Example #3
0
def update_memcache_after_commit(context):
  """
  The memccache entries is updated after DB commit
  Logs error if there are errors in updating entries in cache

  Args:
    context: POST/PUT/DELETE HTTP request or import Converter contextual object
    modified_objects:  objects in cache maintained prior to committing to DB
  Returns:
    None

  """
  if not has_memcache():
    return

  if context.cache_manager is None:
    logger.error("CACHE: Error in initiaizing cache manager")
    return

  cache_manager = context.cache_manager

  related_objs = list()
  for val in getattr(flask.g, "referenced_objects", {}).itervalues():
    obj_list = val.values()
    if obj_list:
      related_objs.append((obj_list[0], None))
  memcache_mark_for_deletion(context, related_objs)

  # TODO(dan): check for duplicates in marked_for_delete
  if cache_manager.marked_for_delete:
    delete_result = cache_manager.bulk_delete(
        cache_manager.marked_for_delete, 0)
    # TODO(dan): handling failure including network errors,
    #            currently we log errors
    if delete_result is not True:
      logger.error("CACHE: Failed to remove collection from cache")

  status_entries = []
  for key in cache_manager.marked_for_delete:
    status_entries.append('DeleteOp:' + str(key))
  if status_entries:
    delete_result = cache_manager.bulk_delete(status_entries, 0)
    # TODO(dan): handling failure including network errors,
    #            currently we log errors
    if delete_result is not True:
      logger.error("CACHE: Failed to remove status entries from cache")

  clear_permission_cache()
  cache_manager.clear_cache()
Example #4
0
def clear_permission_cache():
    """Drop cached permissions for all users."""
    if not has_memcache():
        return

    client = get_cache_manager().cache_object.memcache_client

    # We delete all the cached user permissions as well as
    # the permissions:list value itself
    keys_to_delete = ['permissions:list']
    for user_key in client.get('permissions:list') or set():
        keys_to_delete.append(user_key)
        keys_to_delete.extend(blob_get_chunk_keys(client, user_key))

    client.delete_multi(keys_to_delete)
Example #5
0
def clear_permission_cache():
  """Drop cached permissions for all users."""
  if not has_memcache():
    return

  client = get_cache_manager().cache_object.memcache_client

  # We delete all the cached user permissions as well as
  # the permissions:list value itself
  keys_to_delete = list('permissions:list')
  for user_key in client.get('permissions:list') or set():
    keys_to_delete.append(user_key)
    keys_to_delete.extend(blob_get_chunk_keys(client, user_key))

  client.delete_multi(keys_to_delete)
Example #6
0
def update_memcache_before_commit(context, modified_objects, expiry_time):
    """
  Preparing the memccache entries to be updated before DB commit
  Also update the memcache to indicate the status cache operation
  'InProgress' waiting for DB commit
  Raises Exception on failures, cannot proceed with DB commit

  Args:
    context: POST/PUT/DELETE HTTP request or import Converter contextual object
    modified_objects:  objects in cache maintained prior to committing to DB
    expiry_time: Expiry time specified for memcache ADD and DELETE
  Returns:
    None

  """
    if not has_memcache():
        return

    context.cache_manager = get_cache_manager()

    if modified_objects is not None:
        if modified_objects.new:
            memcache_mark_for_deletion(context, modified_objects.new.items())

        if modified_objects.dirty:
            memcache_mark_for_deletion(context, modified_objects.dirty.items())

        if modified_objects.deleted:
            memcache_mark_for_deletion(context,
                                       modified_objects.deleted.items())

    status_entries = {}
    for key in context.cache_manager.marked_for_delete:
        build_cache_status(status_entries, 'DeleteOp:' + key, expiry_time,
                           'InProgress')
    if status_entries:
        logger.info("CACHE: status entries: %s", status_entries)
        ret = context.cache_manager.bulk_add(status_entries, expiry_time)
        if ret:
            if ret == status_entries:
                logger.error(
                    'CACHE: Unable to add status for newly created entries '
                    'because of Network/RPC/Server errors')
            else:
                logger.error(
                    'CACHE: Newly created entries already exist in cache: %s',
                    ret)
Example #7
0
def clear_users_permission_cache(user_ids):
    """ Drop cached permissions for a list of users. """
    if not has_memcache() or not user_ids:
        return

    client = get_cache_manager().cache_object.memcache_client

    keys_to_delete = list()
    cached_keys_set = client.get('permissions:list') or set()
    for user_id in user_ids:
        key = 'permissions:{}'.format(user_id)
        if key in cached_keys_set:
            cached_keys_set.remove(key)
            keys_to_delete.append(key)
            keys_to_delete.extend(blob_get_chunk_keys(client, key))

    client.set('permissions:list', cached_keys_set)
    client.delete_multi(keys_to_delete)
Example #8
0
def clear_users_permission_cache(user_ids):
  """ Drop cached permissions for a list of users. """
  if not has_memcache() or not user_ids:
    return

  client = get_cache_manager().cache_object.memcache_client

  keys_to_delete = list()
  cached_keys_set = client.get('permissions:list') or set()
  for user_id in user_ids:
    key = 'permissions:{}'.format(user_id)
    if key in cached_keys_set:
      cached_keys_set.remove(key)
      keys_to_delete.append(key)
      keys_to_delete.extend(blob_get_chunk_keys(client, key))

  client.set('permissions:list', cached_keys_set)
  client.delete_multi(keys_to_delete)
Example #9
0
def update_memcache_before_commit(context, modified_objects, expiry_time):
  """
  Preparing the memccache entries to be updated before DB commit
  Also update the memcache to indicate the status cache operation
  'InProgress' waiting for DB commit
  Raises Exception on failures, cannot proceed with DB commit

  Args:
    context: POST/PUT/DELETE HTTP request or import Converter contextual object
    modified_objects:  objects in cache maintained prior to committing to DB
    expiry_time: Expiry time specified for memcache ADD and DELETE
  Returns:
    None

  """
  if not has_memcache():
    return

  context.cache_manager = get_cache_manager()

  if modified_objects is not None:
    if modified_objects.new:
      memcache_mark_for_deletion(context, modified_objects.new.items())

    if modified_objects.dirty:
      memcache_mark_for_deletion(context, modified_objects.dirty.items())

    if modified_objects.deleted:
      memcache_mark_for_deletion(context, modified_objects.deleted.items())

  status_entries = {}
  for key in context.cache_manager.marked_for_delete:
    build_cache_status(status_entries, 'DeleteOp:' + key,
                       expiry_time, 'InProgress')
  if status_entries:
    logger.info("CACHE: status entries: %s", status_entries)
    ret = context.cache_manager.bulk_add(status_entries, expiry_time)
    if ret:
      if ret == status_entries:
        logger.error('CACHE: Unable to add status for newly created entries '
                     'because of Network/RPC/Server errors')
      else:
        logger.error('CACHE: Newly created entries already exist in cache: %s',
                     ret)