Best practices for implementing custom caching? For every instance of every type of entity, I generate a number of caches, named something like: [module_name]__[entity_type]__[entity_id]__[string_depending_on_where_the_cache_came_from]

Now whenever a entity is updated, I want to drop all caches starting with the relevant entity type and id.

How should I store/clear these caches?

Currently I just cache_set(), but that presents a problem when I want to clear, as I don't know the names of all the relevant caches. Is it safe to drop cache entries with a db_delete()?
I can't think of a good reason why dropping the entries manually would cause a problem. This assumes, of course, that you're using MySQL as the backend for your particular cache; although I think the same applies to any other type of cache backend, the method to clear just wouldn't necessarily be a database query.

If you take the core update module as an example, it bypasses the cache_* functions and clears its cache manually:

function _update_cache_clear($cid = NULL, $wildcard = FALSE) {
  if (empty($cid)) {
    db_delete('cache_update')
      // Clear everything except fetch task information because these are used
      // to ensure that the fetch task queue items are not added multiple times.
      -&gt;condition('cid', 'fetch_task::%', 'NOT LIKE')
      -&gt;execute();
  }
  else {
    $query = db_delete('cache_update');
    if ($wildcard) {
      $query-&gt;condition('cid', $cid . '%', 'LIKE');
    }
    else {
      $query-&gt;condition('cid', $cid);
    }
    $query-&gt;execute();
  }
}


I always think "if it's good enough for core, it's good enough for me" :)