php - Yii an Memcache -
We are using memcatch for UI framework and caching.
We have the following problem:
If we request the same query que, then the key is not set before the set of memes (before step 4) we again query the DB.
We want to change this behavior to do something like this:
- We create a DB query q
- If the key for q Memcache exists and the value is not an empty return value
- otherwise if the key is set and the value is set to blank memcache key = & gt;
- Set cache value
- We get results from the memory
Here's the pseudocode and what we want get (q): if q not in memcache: value = query_db (q) memcache [q] = value return memcache [q] else: returm memcache [q] getNew (q): if q memcache Not in: memcache [q] = null value = query_db (q) memcache [q] = memcache and memcache in [q] elif q returns value in memcache [q]! = Null: return memcache [q] else: while true: if memcached [q]! = Null: return memcached [q] else: sleep (3)
In other words, we want to set the memcache key before the results are empty, and other requests for the same query Checks that the value is zero and as long as the value is not zero (this means the query is already processed). Wait
Do you know which part of yii frameworkDD should be modified?
In MySQL, these systems have a widespread locking function that can help: and these e-lock name string.
This idea is a regular Mechcheche check that you already have, then also check whether this exact query has gone into processing using lock name. For this, each query will require a unique lock name. The lock name can be made using
crc32 ($ sql) (in the fastest non-crypto hashing algorbo php), which will create a four character unique hex string for each query.
TIMEOUT can be a large number
getNew (q): if q not in memcache: hash = crc32hex (q) if mysql-is_free (hash) == 0 # If you attach yourself to the synchronous exit of the other thread (mysql-get_lock (hash, TIMEOUT) == 1) # is waiting ... release_lock (hash) # call from yourself so far, in memcache your Value return is getNew (end) endif (mysql-get_lock (hash, TIMEOUT) == 1) value = query_db (q) memcache [q] = value mysql-release_lock (hash) endf returns memory [q] else: returm memcache [Q]
Comments
Post a Comment