How to use PHP to determine whether Memcache Key/value exist method

Source: Internet
Author: User

1.memcache Introduction

Memcache is a set of distributed cache software for data caching and distributed Web site session storage. Use key=>value storage, support data compression storage processing and data expiration processing.
Value supports only string, and if other formats are required, it can be converted to JSON before saving.

How to use:

<?php$m = new Memcache; $m->connect (' localhost ', 11211); $m->set (' foo ', 100); Save data Echo $m->get (' foo '); Reading data?>

Using Memcache as the data cache, the steps are generally
1. Determine if the memcache has data, and if so, get the data from the data source if it is returned directly.
2. After fetching data from the data source, write Memcache as the cache, which is reserved for the next request.

2. Determine if the Memcache data exists

Determine if the memcache has data, usually through the Get method to get the data, based on whether the data false to judge.

<?php$m = new Memcache; $m->connect (' localhost ', 11211), $m->set (' foo '), if ($m->get (' foo ')) {    echo ' Foo exists <br> ';} else{    Echo ' foo does not exist <br> ';} $m->delete (' foo '), if ($m->get (' foo ')) {    echo ' foo exists <br> ';} else{    Echo ' foo does not exist <br> ';}? >

However, this judgment is not rigorous enough, if value is false or null when it is judged to not exist, we can judge whether the existence of the key exists to determine whether the data exists.

3. Determine if Memcache key exists

<?php$m = new Memcache; $m->connect (' localhost ', 11211);//normal judgment echo ' normal value: '; $m->set (' foo '); if (check_ Key_exists ($m, ' foo ')) {echo ' foo key exists <br> ';} else{Echo ' foo key does not exist <br> ';} False to judge Echo ' false to judge: '; $m->set (' foo ', false), if (Check_key_exists ($m, ' foo ')} {echo ' foo key exists <br> ';} else{Echo ' foo key does not exist <br> ';} Null judgment echo ' null judgment: '; $m->set (' foo ', null); if (Check_key_exists ($m, ' foo ')} {echo ' foo key exists <br> ';} else{Echo ' foo key does not exist <br> ';} After the data expires, 1 seconds expires echo ' Expiration judgment: '; $m->set (' foo ', ' 0 ', ' 1 '); sleep (2); if (Check_key_exists ($m, ' foo ')) {echo ' foo key exists < Br> ';} else{Echo ' foo key does not exist <br> ';} After the delete judgment echo ' Delete after judgment: '; $m->set (' foo '); $m->delete (' foo '); if (Check_key_exists ($m, ' foo ') {echo ' foo Key exists <br> ';} else{Echo ' foo key does not exist <br> ';} /** * @param obj $m memcache obj * @param String $name Key Name * @return Boolean */function check_key_exists ($m , $name) {$data = $M->get ($name); return $data!==false;}? >

Output:
Normal judgment: Foo key exists
False judgment: Foo key exists
Null judgment: Foo key exists
Expiration judgment: Foo key does not exist
Delete after the judgment: Foo key does not exist

Only if the key is deleted or has expired is not known to exist, and other cases, even if value is Null/false, are present.

Note If the value of value is false, use the Memcache get method to get to the empty string , because memcache can only hold string data. So false is converted to an empty string when it is saved.

This article explains how to use PHP to judge Memcache Key/value whether there is a method, more relevant content please pay attention to the PHP Chinese web.

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.