Design pattern for memcached data caching

前端 未结 4 706
盖世英雄少女心
盖世英雄少女心 2021-02-03 11:51

It\'s easy to wrap optional memcached caching around your existing database queries. For example:

Old (DB-only):

function getX
    x = get from db
    r         


        
4条回答
  •  花落未央
    2021-02-03 12:06

    Here's my understanding of how NHibernate (and therefore probably Hibernate) does it. It has 4 caches:

    • row cache: this caches DB rows. The cache key is TableName#id, the other entries are the row values.
    • query cache: this caches the results returned for a particular query. The cache key is the query with parameters, the data is a list of the TableName#id row keys that were returned as query results.
    • collections cache: this caches the child objects of any given parent (which NHibernate allows to be lazy-loaded.) So if you access myCompany.Employees, the employees collection will be cached in the collections cache. The cache key is CollectionName#entityId, the data is a list of the TableName#id row keys for the child rows.
    • table update cache: a list of each table and when it was last updated. If a table was updated after the data was cached, the data is considered stale.

    This is a pretty flexible solution, is very efficient space-wise, and guarantees that the data won't be stale. The disadvantage is that a single query can require several round-trips to the cache, which can be a problem if the cache server is on the network.

提交回复
热议问题