Memcache is a high-performance distributed memory object caching system that can be used to store data in a variety of formats, including images, videos, files, and database retrieval results, by maintaining a unified, huge hash table in memory. The simple thing is to call the data into memory and then read it from memory, which greatly improves the reading speed. The SAE also provides memcache caching services.
How do I open the SAE Memcache service?
Note: We recommend that you do not start too large a memcache quota because, as appropriate for your project, the general recommendation does not need to be open for more than 20M.
The use of SAE Memcache is illustrated by a small example and a comprehensive example.
Instance 1,sae memcache connection, data insertion and removal
<? PHP $link = memcache_init (); Memcache_set ($link, ' lazy ', ' a lazy people ', 0, 30); // Set $re = memcache_get ($link, ' lazy '); Var_dump ($re);? >
Note: The SAE only needs to call Memcache_init () (no parameters, the function completes the Memcache Connect task), then the memcache can be used normally.
Execute script output:
String ("A Lazy people")
Test Address: http://lazydemo.sinaapp.com/memcache/memcache_test.php
An instance that implements the API interface minute quota using Memcache
In fact, Memcache has memcache_increment () function and cache expiration, in line with these two, you can achieve now very common example of minute quotas, we can boldly guess, Sina Weibo open platform interface is based on this implementation, give code.
<?PHPDefine(' Ratemax ', ' 10 ');//Configure minutes to access up to 10 times$link=memcache_init ();$minute _now=Date(' I ', Time());//take the current number of minutes$key= ' rate '.$minute _now;if(Memcache_get ($link,$key) ==NULL) {Memcache_set ($link,$key, ' 1 ', 0, 60);//Set//Note 60 Here is the cache expiration time, 60 seconds is 1 minutes Exit(0);}$re= Memcache_get ($link,$key);Var_dump($re);if($re>Ratemax) { die(' Minute rate limit! ');}Else{memcache_increment ($link,$key, 1);}?>
Refresh http://lazydemo.sinaapp.com/memcache/api_rate.php within one minute
String (2) "one" Minute rate limit!
This tutorial source code package download
Http://lazydemo.sinaapp.com/memcache/memcache.zip
Sina App Engine (SAE) Introductory Tutorial (6)-Memcache use