Getting started with Memcached in php _ php tips-php Tutorial

Source: Internet
Author: User
This article mainly introduces the Memcached entry-level instance in php and analyzes in detail the concept and usage of memcached, which is a very practical technique, for more information about Memcached in php, see this article. Share it with you for your reference. The details are as follows:

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 strategy, to find a way to make memcached cache those queries that affect performance as much as possible, you can try to make some execution time logs for all queries in the application, it helps you analyze what content is to be cached.

Now, if you are running an e-commerce website, you can cache product descriptions, shipping information, or other data that requires complex queries in memcached, when a product page is loaded, the data mentioned above will skip database queries 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 updating 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 low cache hit rate may lead to some extra 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 you do not have any permission control procedures during the cache access process. if your data is not very important, you don't have to worry about security issues in this area. if you need it, you can use it more completely:

Use a unique key:Because the data in memcached exists in the form of a large array, you should use a unique key. the only way to access your data is through the key when you save the data, in addition, there is no other way to query.

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 and which servers are allowed to be accessed, which are not allowed to be accessed.

Encrypt your data: you can save the data and key in memcached encrypted mode, which takes some extra cpu time, but for your data security, this method is worth your effort when conditions permit.

The code is as follows:

<? Php
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 ');
?>

I hope this article will help you with php programming.

Related Article

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.