开发者

Is memcached all about timing?

I beleive the standard way to cache something with mamcached is to insert the object into the cache for a set period of time e.g.

 memcli:set(key, rows_array, 5 * 60)

Is there not a better way to cache things where the cache will check inside the database to see if the data开发者_JAVA百科 has changed rather than relying on a timer which could cause sync issues?

I'm going to use PHP.


The cache will not check the database, because that is contrary to the idea of caching.
Instead you could either update or remove objects from the cache when you change their value in the database.


If data is subject to be modified in the database, not controlled by your application where you can implement your cache in write-through fashion, then that data is probably not a good candidate for caching (assuming you can live with the stale data until it is evicted).


The cache certainly does not check the database. You have to do that yourself. You know how the tables in the database are related to each other and what tables you need to pull information from to render a page. Do that (or don't if you are satisfied with what you told mamcached).


Is there not a better way to cache things where the cache will check inside the database to see if the data has changed rather than relying on a timer which could cause sync issues?

That timer is not because of checking of database, but to free up memory(evicting data from the cache).

From google app engine(python):

Memcache is typically used with the following pattern: The application receives a query from the user or the application. The application checks whether the data needed to satisfy that query is in memcache. If the data is in memcache, the application uses that data. If the data is not in memcache, the application queries the datastore and stores the results in memcache for future requests. The pseudocode below represents a typical memcache request:

def get_data():
    data = memcache.get("key")
    if data is not None:
        return data
    else:
        data = self.query_for_data()
        memcache.add("key", data, 60)
        return data

On update of key(database) you will also have to do an update to memcache key.


You may want to consider using redis. While not persistant (semi), this will offer some data storage mechanism and has a fast performance.

http://www.redis.io

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜