Home Backend Development PHP Tutorial 在php中设置session用memcache来存储的方法总结_PHP

在php中设置session用memcache来存储的方法总结_PHP

May 28, 2016 am 11:49 AM
memcache

memcached提供了一个自定义的session处理器可以被用于存储用户session数据到memcached服务端。一个完全独立的memcached实例将会在内部使用,因此如果需要您可以设置一个不同的服务器池。session的key被存储在前缀memc.sess.key.之下,因此, 如果你对session和通常的缓存使用了同样的服务器池,请注意这一点。译注:另外一个session和通常缓存分离的原因是当通常的缓存占满了memcached服务端后,可能会导致你的session被从缓存中踢除,导致用户莫名的掉线。

session.save_handler string

设置为memcached开启memcached的session处理器。

session.save_path string

定义一个逗号分隔的hostname:port样式的session缓存服务器池,例如: "sess1:11211, sess2:11211".

方法I: 在 php.ini 中全局设置

session.save_handler = memcache 
session.save_path = "tcp://127.0.0.1:11211" 
Copy after login

方法II: 某个目录下的 .htaccess

php_value session.save_handler "memcache" 
php_value session.save_path "tcp://127.0.0.1:11211" 
Copy after login

方法III: 再或者在某个一个应用中

ini_set("session.save_handler", "memcache"); 
ini_set("session.save_path", "tcp://...:"); 
Copy after login

使用多个 memcached server 时用逗号","隔开,并且和 Memcache::addServer() 文档中说明的一样,可以带额外的参数"persistent"、"weight"、"timeout"、"retry_interval" 等等,类似这样的:"tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2" 。

如果安装的PECL是memcached(依赖libmemcached库的那个扩展),则配置应为

ini_set("session.save_handler", "memcached"); // 是memcached不是memcache 
ini_set("session.save_path", "127.0.0.1:11211"); // 不要tcp:[/b]
Copy after login

代码例子(不依赖libmemcached库的那个)

<&#63;php 
session_start(); 
if (!isset($_SESSION['TEST'])) { 
$_SESSION['TEST'] = time(); 
} 
$_SESSION['TEST'] = time(); 
print $_SESSION['TEST']; 
print "<br><br>"; 
print $_SESSION['TEST']; 
print "<br><br>"; 
print session_id(); 
&#63;> 

Copy after login

用 sessionid 去 memcached 里查询一下:

<&#63;php 
$memcache = memcache_connect('localhost', ); 
var_dump($memcache->get('ccedecbceebe')); 
$memcache->set('aaaa', 'hello everyone'); 
var_dump($memcache->get('aaaa')); 
&#63;>
Copy after login

会看到

string(37) "TEST|i:1177556731;TEST3|i:1177556881;"

这样的输出,证明 session 正常工作。

下面通过两种用法实例给大家介绍下php中使用memcached来存储session

一、

ini_set("session.save_handler", "memcache");
ini_set("session.save_path","tcp://127.0.0.1:11211");
Copy after login

多个memcached

ini_set("session.save_path","tcp://127.0.0.1:11211,tcp://127.0.0.1:11211");
Copy after login

二、

ini_set("session.save_handler", "memcached");
ini_set("session.save_path","...:");
Copy after login

多个memcached

ini_set("session.save_path","127.0.0.1:11211,127.0.0.1:11211");
Copy after login

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
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
How to use Memcache in PHP development? How to use Memcache in PHP development? Nov 07, 2023 pm 12:49 PM

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to use Memcache for efficient data writing and querying in PHP development? How to use Memcache for efficient data writing and querying in PHP development? Nov 07, 2023 pm 01:36 PM

How to use Memcache for efficient data writing and querying in PHP development? With the continuous development of Internet applications, the requirements for system performance are getting higher and higher. In PHP development, in order to improve system performance and response speed, we often use various caching technologies. One of the commonly used caching technologies is Memcache. Memcache is a high-performance distributed memory object caching system that can be used to cache database query results, page fragments, session data, etc. By storing data in memory

How to use Memcache for efficient data reading and writing operations in PHP development? How to use Memcache for efficient data reading and writing operations in PHP development? Nov 07, 2023 pm 03:48 PM

In PHP development, using the Memcache caching system can greatly improve the efficiency of data reading and writing. Memcache is a memory-based caching system that can cache data in memory to avoid frequent reading and writing of the database. This article will introduce how to use Memcache in PHP for efficient data reading and writing operations, and provide specific code examples. 1. Install and configure Memcache First, you need to install the Memcache extension on the server. able to pass

Practice and thinking on optimizing data interaction with Memcache caching technology in PHP Practice and thinking on optimizing data interaction with Memcache caching technology in PHP May 17, 2023 pm 09:51 PM

The practice and thinking of Memcache caching technology to optimize data interaction in PHP In modern Web applications, data interaction is a very important issue. It is not efficient enough and will limit the scalability and performance of Web applications. In order to speed up data interaction, our usual approach is to optimize the design of the database, improve the performance of the hardware and increase the server capacity. However, these methods all have a common limitation: they increase the cost of the system. In recent years, Memcache technology has made progress in solving this problem.

Application and practice of Memcache caching technology in PHP projects Application and practice of Memcache caching technology in PHP projects May 17, 2023 pm 02:10 PM

Memcache is an open source, distributed caching technology. It greatly improves the speed of data access by storing data in memory, thus improving the performance and responsiveness of the website. In PHP projects, Memcache caching technology is also widely used and has achieved good results. This article will deeply explore the application and practice of Memcache caching technology in PHP projects. 1. Principles and advantages of Memcache Memcache is a memory caching technology that can store data

Utilizing Memcache caching technology to improve the concurrent processing capabilities of PHP applications Utilizing Memcache caching technology to improve the concurrent processing capabilities of PHP applications May 18, 2023 am 08:12 AM

With the rapid development of the Internet, more and more applications need to face a large number of concurrent requests. How to improve the concurrent processing capabilities of applications has become a problem that developers need to solve. Among them, using Memcache caching technology for concurrency optimization has become a relatively popular solution. Memcache is an efficient caching technology suitable for large-scale web applications, databases and distributed systems. Its characteristic is to store data in memory to achieve high-speed read and write operations. During the data access process of web applications,

How to use Memcache for distributed caching in PHP development? How to use Memcache for distributed caching in PHP development? Nov 07, 2023 pm 03:04 PM

As web applications become increasingly complex, performance has become a critical issue. In many applications, database queries are one of the most time-consuming operations. In order to avoid frequently reading data from the database, a caching system can be used to store frequently read data in memory for quick access. In PHP development, using Memcached for distributed caching is an extremely common practice. In this article we will introduce how to use Memcached for distributed caching. What is Memca

How to use Memcache to optimize data storage operations in your PHP application? How to use Memcache to optimize data storage operations in your PHP application? Nov 08, 2023 pm 09:06 PM

How to use Memcache to optimize data storage operations in your PHP application? In web application development, data storage is a crucial link. In PHP applications, Memcache, as a memory cache system, can effectively improve the efficiency of data storage and reading operations. This article will introduce how to use Memcache to optimize data storage operations in PHP applications, and attach specific code examples. Step 1: Install the Memcache extension First, you need to install Me in your PHP environment

See all articles