Memcached Getting Started code instance _php tutorial

Source: Internet
Author: User
Memcached Getting Started code instances


Class Mycache
{
Private $cache;
function __construct ()
{
$this->cache = new Memcache ();
You can replace the 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 ');

?>


Under what circumstances should memcache be used, and under what circumstances?
When should you use Memcache and when should you avoid using it? Now that you know, memcahced is designed to relieve the stress of the database tutorial. But you'd better develop a good strategy to get memcached to cache as much of the queries that affect performance as possible. You can try to do some execution time logs for all queries in your app, which can help you analyze what is going to be focused on caching.

Now let's say you're running an ecommerce site. You can cache product introductions, shipping information, or other data that require complex queries in memcached, and so on. When a product page is loaded, the data mentioned above will skip the database query and be taken directly from the cache. Cache can greatly change the overall performance of your site, you just need to remember to update the product in the background when the cache is updated on the line.

In some cases, it is not a good idea to cache data, such as when a data is updated frequently, every time the data is updated, we need to update the cache at the same time, the cache hit rate is not high, resulting in some additional performance sacrifices. In this case, it might be better to look directly into the database.

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

Use unique key: Because the data in memcached is in a large array form, you should use a unique key. The only way to access your data is through the key when you save the data, and there is no other way to query.
Keep your memcached safe: Because the memcached itself does not have an authentication mechanism, the server query for memcached should be done through a firewall. 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 by encrypting it. This will take some extra CPU time, but for your data to be safe, this method is worth trying for if circumstances permit.


http://www.bkjia.com/PHPjc/444848.html www.bkjia.com true http://www.bkjia.com/PHPjc/444848.html techarticle memcached Introductory code instance? PHP Tutorial class Mycache {private $cache; function __construct () {$this-cache = new Memcache ();//You Can replace localhost by memcached server IP a ...

  • 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.