Respect knowledge, reprinted please indicate this article from: programming artist poechant csdn blog http://blog.csdn.net/poechant
"Back-end server development series-" Practical memcached memory cache system "series blog: http://blog.csdn.net/poechant/article/category/1060687
Memcached is rarely used in real-time read/write scenarios. However, after memcached is written, read operations are performed only after a certain period of time. But what if there is an instant read operation after writing in the Application Scenario? There seems to be nothing special. We can still write it like this:
Note: The spymemcached client is used here.
MemcachedClient cache = new MemcachedClient(cacheServerAddr);cache.set("key", 3600, bigData);return cache.get("key");
After the data is written to the cache, if other clients perform the read operation immediately, the read will fail because the set operation is asynchronous (async) and may not be completed yet.
A feasible method is synchronous write operations. The common set method does not use this method. Therefore, you must use write operations that comply with the memcached CAS (check and set) protocol. This write operation is generally based on the cas id obtained after reading (similar to the version ID in SVN). According to this cas ID, there is no "Duplicate write" conflict with other write operations. Therefore, the CAS protocol can be used as follows:
(1) initial write: write a simple initial value (set, asynchronous operation );
(2) Get version: Obtain the cas id asynchronously;
(3) Synchronous write: Write Data in synchronous mode to ensure that the write has ended before reading.
MemcachedClient cache = new MemcachedClient(cacheServerAddr);cache.set(“key”, 3600, "");long casId = cache.asyncGets("key").get().getCas();cache.cas("key", casid, bigData);return cache.get("key");
The "Set-asyncgets-cas" method is used to solve the problem of asynchronous real-time read/write in cache. We call it "sac" (you must have thought of zookeeper ).
Respect knowledge, reprinted please indicate this article from: programming artist poechant csdn blog http://blog.csdn.net/poechant
-