Memcached entry code example

Source: Internet
Author: User

Memcached entry code example

<? Php tutorial
 
Class mycache
{
Private $ cache;
Function _ construct ()
{
$ This-> cache = new memcache ();
// You can replace localhost by memcached server ip addr and port no.
$ This-> cache-> connect ('localhost', 10987 );
}
 
Function get_data ($ key)
{
$ Data = $ this-> cache-> get ($ key );
If ($ data! = Null)
Return $ data;
Else
{
If ($ this-> cache-> getresultcode () = memcached: res_notfound)
{
// Do the databse query here and fetch data
$ This-> cache-> set ($ key, $ data_returned_from_database );
}
Else
{
Error_log ('no data for key'. $ key );
}
}
}
}
 
$ Cache = mycache ();
$ Cache-> get_data ('foo ');
 
?>


When is memcache used or not used?
When should you use memcache and avoid using it? Now you know that memcahced is designed to reduce the pressure on the database tutorial end. But you 'd better develop a good policy to find a way for memcached to cache the queries that affect performance as much as possible. You can try to make some execution time logs for all queries in the application to help you analyze the content that is to be cached.

Now suppose you are running an e-commerce website. You can cache product introduction, shipping information, or other data that requires complex queries in memcached. When a product page is loaded, the data mentioned above will skip the database query and be obtained directly from the cache. The cache can greatly change the overall performance of your website. You only need to remember to update these caches when you update products in the background.

In other cases, caching data is not a good idea. For example, when a data is frequently updated, We need to update the cache at the same time for every data update, the cache hit rate is not high, leading to some additional performance sacrifices. In this case, it may be better to directly query the database.

Security of memcached
If you understand the memcached workflow, you may have noticed that there is no permission control flow during the cache access process. If your data is not very important, you don't have to worry about security issues. If you need it, the following points can help you fully use it:

Unique key: because data in memcached exists in a large array, you should use a unique key. The only way to access your data is by using the key when you save the data, there is no other way to query it.
Ensure the security of your memcached: Because memcached does not have an authentication mechanism, you should use the firewall to query memcached servers. You can set rules on the firewall, which servers are allowed to be accessed and which are not allowed to be accessed.
Encrypt Your Data: You can save the data and key in memcached encrypted mode. This takes some extra cpu time, but for your data security, this method is worth trying if it is allowed.


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.