Use of memcached

Jul 28, 2016 am 08:27 AM
function gt key this

Basic usage

<?php
/**
 * Created by PhpStorm.
 * User: raid
 * Date: 2016/7/6
 * Time: 12:03
 */

$m = new Memcached;
$arr = array(
    array(&#39;127.0.0.1&#39;, 11211),
    array(&#39;127.0.0.2&#39;, 11211),
);
$m->addServer('127.0.0.1', 11211);
print_r($m->getStats());
echo "<br/>";
print_r($m->getVersion());
echo "<br/>";


$data = array(
    'key' => 'value',
    'key2' => 'value2',
);
//$m->setMulti($data, 600);
$result = $m->getMulti(array('key', 'key2'));
$m->deleteMulti(array('key', 'key2'));
//print_r($result);
echo $m->get('key');

echo $m->getResultCode();
echo $m->getResultMessage();

//$m->add('mkey', 'mvalue', 600);
//$m->replace('mkey', 'mvalue2', 1);

//$m->flush();

//$m->set('num', 50, 600);

//$m->increment('num', 5);

$m->decrement('num', 5);

echo $m->get('num');

$m->flush();
Copy after login

Packaging class:

<?php

/**
 * Created by PhpStorm.
 * User: raid
 * Date: 2016/7/6
 * Time: 19:25
 */
class Mem {
    //Memcached对象
    private $m;
    //对象类型
    private $type = &#39;Memcached&#39;;
    //缓存时间
    private $time = 0;
    //错误
    private $error;
    //是否开启调试模式
    private $debug = &#39;true&#39;;

    public function __construct() {
        if (!class_exists($this->type)) {
            $this->error = 'No '.$this->type;
            return false;
        } else {
            $this->m = new $this->type;
        }
    }

    public function addServer($arr) {
        $this->m->addServers($arr);
    }

    public function s($key, $value = '', $time = NULL) {
        $number = func_num_args();
        if ($number == 1) {
            //get操作
            return $this->get($key);
        } else if ($number >= 2) {
            if ($value === NULL) {
                //delete操作
                $this->delete($key);
            } else {
                //set操作
                $this->set($key, $value, $time);
            }

        }
    }

    private function set($key, $value, $time = NULL) {
        if ($time === NULL) {
            $time = $this->time;
        }
        $this->m->set($key, $value, $time);
        if ($this->debug) {
            if ($this->m->getResultCode() != 0) {
                return false;
            }
        }
    }

    private function get($key) {
        $ret = $this->m->get($key);
        if ($this->debug) {
            if ($this->m->getResultCode() != 0) {
                return false;
            }
        }

        return $ret;
    }

    /**
     * 删除
     * @param $key
     */
    private function delete($key) {
        $this->m->delete($key);
    }

    public function getError() {
        if ($this->error) {
            return $this->error;
        } else {
            return $this->m->getResultMessage();
        }
    }

    public function d($debug) {
        $this->debug = $debug;
    }



}
Copy after login

Usage of packaging class:

<?php
/**
 * Created by PhpStorm.
 * User: raid
 * Date: 2016/7/6
 * Time: 19:06
 */

include &#39;Mem.class.php&#39;;

$m = new Mem();

$m->addServer(array(
    array('127.0.0.1', 11211),
));

//$m->s('key', 'value', 1800);
//
//$m->s('key', NULL);
//echo $m->s('key');
//echo $m->getError();

$m->s('test', 'testvalue', 0);
echo $m->s('test');
echo "<br/>";
$m->s('test', NULL);
echo $m->s('test');
Copy after login

The above has introduced the use of memcached, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1234
24
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

How SpringBoot monitors redis Key change events How SpringBoot monitors redis Key change events May 26, 2023 pm 01:55 PM

1. Function Overview Keyspace notification allows clients to receive events that modify Rediskey changes in some way by subscribing to channels or patterns. All commands that modify key keys. All keys that received the LPUSHkeyvalue[value…] command. All expired keys in the db database. Events are distributed through Redis's subscription and publishing functions (pub/sub), so all clients that support subscription and publishing functions can directly use the keyspace notification function without any modifications. Because the current subscription and publishing functions of Redis adopt a fireandforget strategy, if your program

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to solve the problem of batch deletion of key values ​​in redis How to solve the problem of batch deletion of key values ​​in redis May 31, 2023 am 08:59 AM

Problems encountered: During the development process, you will encounter keys that need to be deleted in batches according to certain rules, such as login_logID (ID is a variable). Now you need to delete data such as "login_log*", but redis itself only has batch query. Command keys for class key values, but there is no command for batch deletion of a certain class. Solution: Query first, then delete, use xargs to pass parameters (xargs can convert pipe or standard input (stdin) data into command line parameters), execute the query statement first, and then remove the queried key value and the original del parameters. delete. redis-cliKEYSkey* (search condition)|xargsr

Unpatchable Yubico two-factor authentication key vulnerability breaks the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices Unpatchable Yubico two-factor authentication key vulnerability breaks the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices Sep 04, 2024 pm 06:32 PM

An unpatchable Yubico two-factor authentication key vulnerability has broken the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices. The Feitian A22 JavaCard and other devices using Infineon SLB96xx series TPMs are also vulnerable.All

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

See all articles