Sina App Engine (SAE) Introductory Tutorial (6)-Memcache use

Source: Internet
Author: User

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 10 times within a minute to see:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.