Table of Contents
php使用memcached简单示例分享,phpmemcached示例
Home php教程 php手册 php使用memcached简单示例分享,phpmemcached示例

php使用memcached简单示例分享,phpmemcached示例

Jun 13, 2016 am 08:42 AM
Multidimensional Arrays

php使用memcached简单示例分享,phpmemcached示例

本文章简单向码农们介绍一下memcached。memcached 是高效、快速的分布式内存对象缓存系统,主要用于加速WEB动态应用程序。今天我和码农们先来简单探讨下memcached的用法。

1.添加memcached扩展包

<code class="hljs">php_memcache.dll</code>
Copy after login

2.在PHP.INI添加memcached扩展

<code class="php hljs">extension=php_memcache.dll</code>
Copy after login

3.程序

<?<span>php
    </span><span>//</span><span>创建一个mem对象实例</span>
    <span>$mem</span>=<span>new</span><span> Memcache;
    </span><span>if</span>(!<span>$mem</span>->connect("10.18.110.213",11211<span>)){
        </span><span>die</span>('连接失败!'<span>);
    }
    </span><span>//</span><span>增加
    //1.增加一个字串</span><span>
/*</span><span>    if($mem->set('key1',"beijing",MEMCACHE_COMPRESSED,60)){
        echo '添加ok';
    }</span><span>*/</span>
    <span>//</span><span>2.添加数值</span><span>
/*</span><span>    if($mem->set('key1',100,MEMCACHE_COMPRESSED,60)){
        echo '添加ok';
    }</span><span>*/</span>
    <span>//</span><span>3.添加数组
    //在添加数组是,根据需要. 希望序列号放入  ,
    //serialize<=>unserialize, 如果根据需要,也可以json_encode <=> json_decode</span>
    <span>$arr</span>=<span>array</span>("bj",'tj'<span>);
    </span><span>if</span>(<span>$mem</span>->set('key1',<span>$arr</span>,MEMCACHE_COMPRESSED,<span>time</span>()+31*3600*24<span>)){
        </span><span>echo</span> '添加数组ok99111'<span>;
    }
    </span><span>//</span><span>4.添加对象</span><span>
/*</span><span>    class Dog{
        public $name;
        public $age;
        public function __construct($name,$age){
            $this->name=$name;
            $this->age=$age;
        }
    }
    $dog1=new Dog('小狗',50);
    if($mem->set('key1',$dog1,MEMCACHE_COMPRESSED,60)){
        echo '添加对象ok';
    }</span><span>*/</span>
    <span>//</span><span>5.添加null 布尔值</span><span>
/*</span><span>    if($mem->set('key1',false,MEMCACHE_COMPRESSED,60)){
        echo '添加布尔ok';
    }</span><span>*/</span>
    <span>//</span><span>6. 资源类型放入.</span><span>
/*</span><span>    $con=mysql_connect("127.0.0.1","root","root");
    if(!$con){
        die('连接数据库失败');
    }
    var_dump($con);
    echo "<br/>";
    if($mem->set('key1',$con,MEMCACHE_COMPRESSED,60)){
        echo '添加资源ok';
    }</span><span>*/</span>
    <span>//</span><span>查询</span>
    <span>$val</span>=<span>$mem</span>->get('key1'<span>);
</span><span>//</span><span>修改
    //可以使用replace</span>
    <span>if</span>(<span>$mem</span>->replace("key11",'hello',MEMCACHE_COMPRESSED,60<span>)){
        </span><span>echo</span> 'replace ok'<span>;
    }</span><span>else</span><span>{
        </span><span>echo</span> 'replace no ok'<span>;
    }
    </span><span>//</span><span>删除</span>
    <span>echo</span> "<br/>"<span>;
    </span><span>if</span>(<span>$mem</span>->delete('key14'<span>)){
        </span><span>echo</span> 'key14 删除'<span>;
    }</span><span>else</span><span>{
        </span><span>echo</span> 'key14不存在'<span>;
    }</span>
Copy after login

原文地址:http://www.manongjc.com/article/683.html

相关阅读:

php扩展模块memcached长连接使用方法分析

php模块memcached使用指南

PHP5.5在windows安装使用memcached服务端的方法

php Memcached的使用方法介绍

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional Dimensional journey of PHP multi-dimensional array sorting: from one dimension to multi-dimensional Apr 29, 2024 pm 09:09 PM

One-dimensional arrays are sorted using the sort() function, two-dimensional arrays are sorted by internal elements using the usort() function, and high-dimensional arrays are sorted by hierarchical elements using the multi-layer nested usort() function. Solving the decomposition problem layer by layer is the key.

Python program to multiply two matrices using multidimensional arrays Python program to multiply two matrices using multidimensional arrays Sep 11, 2023 pm 05:09 PM

A matrix is ​​a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an mXn matrix, and m and n are called its dimensions. A matrix is ​​a two-dimensional array created in Python using lists or NumPy arrays. In general, matrix multiplication can be done by multiplying the rows of the first matrix by the columns of the second matrix. Here, the number of columns of the first matrix should be equal to the number of rows of the second matrix. Input and output scenario Suppose we have two matrices A and B. The dimensions of these two matrices are 2X3 and 3X2 respectively. The resulting matrix after multiplication will have 2 rows and 1 column. [b1,b2][a1,a2,a3]*[b3,b4]=[a1*b1+a2*b2+a3*a3][a4,a5,a6][b5,b6][a4*b2+a

How to combine multiple arrays into one multidimensional array in PHP How to combine multiple arrays into one multidimensional array in PHP Jul 09, 2023 pm 01:08 PM

How to merge multiple arrays into a multi-dimensional array in PHP In PHP development, we often encounter the need to merge multiple arrays into a multi-dimensional array. This operation is very useful when operating large data collections and can help us better organize and process data. This article will introduce you to several common methods to achieve this operation, and attach code examples for reference. Method 1: Use the array_merge function. The array_merge function is a commonly used array merging function in PHP. It can merge multiple arrays.

How to use array_walk_recursive function in PHP to perform recursive operations on multi-dimensional arrays How to use array_walk_recursive function in PHP to perform recursive operations on multi-dimensional arrays Jun 26, 2023 am 11:40 AM

Arrays are a very common data type in PHP. Sometimes, we will face situations involving multi-dimensional arrays. In this case, if we need to perform the same operation on all elements, we can use the array_walk_recursive() function. The array_walk_recursive() function is a very powerful recursive function in PHP that can help us perform recursive operations on multi-dimensional arrays. It can recursively traverse each element of a multi-dimensional array and perform corresponding operations on it.

Efficient way to reverse multidimensional PHP array Efficient way to reverse multidimensional PHP array Apr 29, 2024 am 09:00 AM

Two effective ways to reverse multidimensional PHP arrays: Recursively using the array_reverse() function: Recursively reverse the elements of each nested array. PHP7's array_reverse() function: Use the new overload of the array_reverse() function to reverse multi-dimensional arrays.

How to sort multidimensional array in PHP How to sort multidimensional array in PHP Jul 07, 2023 pm 12:45 PM

How to sort multi-dimensional arrays in PHP In PHP, arrays are a very common and important data structure, and multi-dimensional arrays are used more frequently in some complex data processing. However, sorting multidimensional arrays can be tricky. This article will show you how to sort multidimensional arrays in PHP and provide specific code examples. Before we begin, let's first understand the structure of multidimensional arrays. Multidimensional array means that the elements in an array are also an array, forming a nested structure. For example: $st

In-depth discussion of PHP arrays: comprehensive analysis of multi-dimensional arrays, associative arrays, etc. In-depth discussion of PHP arrays: comprehensive analysis of multi-dimensional arrays, associative arrays, etc. Mar 13, 2024 pm 02:36 PM

In-depth discussion of PHP arrays: comprehensive analysis of multi-dimensional arrays, associative arrays, etc. Arrays in PHP are a very important data structure that can store multiple data items and access them through indexes. In PHP, arrays can be divided into different types such as indexed arrays, associative arrays, and multidimensional arrays. Each type has its own unique uses and characteristics. This article will delve into the various types of PHP arrays, including how to declare, access, traverse and operate arrays, and will provide specific code examples to help readers better understand. 1. Index array index number

How to debug multidimensional arrays in PHP functions? How to debug multidimensional arrays in PHP functions? Apr 17, 2024 pm 12:39 PM

When debugging multidimensional arrays in PHP, you can view the structure and contents by using var_dump() or print_r(), convert to JSON format using json_encode(), or use Xdebug for advanced debugging. For example, when looking for a missing value in an array, by setting a breakpoint and using var_dump() to examine the value of the variable, you can identify possible reasons why the function cannot find the required value.

See all articles