PHP memcached Primer Case _php Tips

Source: Internet
Author: User
Tags memcached php memcached

This article describes in detail the introduction of PHP under the memcached knowledge and examples. Share to everyone for your reference. Specifically as follows:

Under what circumstances should memcache be used and under what circumstances?

When should you use memcache and when to avoid it? Now that you know, memcahced is designed to ease the pressure on the database tutorial, but you'd better make a good strategy to get memcached to cache the most performance-impacting queries possible, and you can try to do some execution time logs for all the queries in the application. can help you to analyze which content is to be focused and cached.

Now suppose you're running an E-commerce site where you can cache product profiles, shipping information, or other data that requires complex queries, and so on, when a product page is loaded, the data mentioned above will skip the database query and get it directly from the cache, Caching can greatly change the overall performance of your site, you only need to remember to update the product in the background, the cache together to update on the line.

In some cases, caching data is not a good idea, such as when a data is frequently updated, every time the update of the data, we need to update the cache, cache hit rate is not high, resulting in some additional performance sacrifices, in this case, perhaps directly to the database will be better.

Security of Memcached

If you understand the work flow of memcached, you may have noticed that in the process of accessing the cache, there is no permission to control the relevant process, if your data is not very important, you do not have to worry about this security issues, if you need, the following points can help you to use it more fully:

use a unique key: because the data in the memcached is in a large array form, you should use the only key, the only way to access your data is through the key you saved the data, there is no other way to query.

ensure your memcached security: because memcached itself does not have authentication mechanism, so the server query for memcached, should be through the firewall, you can set the rules on the fire wall, which server is allowed to access, Which are not allowed to be accessed.

Encrypt your data: You can store the data and key in the memcached in an encrypted way, which takes some extra CPU time, but for your data to be safe, this method is worth trying, if circumstances permit.

Copy Code code as follows:
<?php
Class Mycache
{
Private $cache;
function __construct ()
{
$this->cache = new Memcache ();
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 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 your PHP program design.

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.