您的位置:首页 > 数据库 > Memcache

php ci框架 里面使用 memcache 存 session

2015-07-15 20:41 786 查看
传送门
https://github.com/cnsaturn/codeigniter-my-session http://blog.csdn.net/weilee2009/article/details/7658260
又一个使用PHP原生Session机制来代替CI默认Cookie机制Session的解决方案;同时,如果服务器支持memcached,那么本类也可以使用Memcached作为Session文件的存储容器(默认使用文本文件),从而极大提升程序性能。同时,它与CI默认Session使用方法完全一致。无需因为使用此库而修改任何目前的代码。
http://php.net/memcached

Memcached

简介
安装/配置

需求
安装
运行时配置
资源类型

预定义常量
超时时间
回调

结果回调
通读缓存回调

Sessions支持
Memcached — Memcached类

Memcached::add — 向一个新的key下面增加一个元素
Memcached::addByKey — 在指定服务器上的一个新的key下增加一个元素
Memcached::addServer — 向服务器池中增加一个服务器
Memcached::addServers — 向服务器池中增加多台服务器
Memcached::append — 向已存在元素后追加数据
Memcached::appendByKey — 向指定服务器上已存在元素后追加数据
Memcached::cas — 比较并交换值
Memcached::casByKey — 在指定服务器上比较并交换值
Memcached::__construct — 创建一个Memcached实例
Memcached::decrement — 减小数值元素的值
Memcached::decrementByKey — Decrement numeric item's value, stored on a specific server
Memcached::delete — 删除一个元素
Memcached::deleteByKey — 从指定的服务器删除一个元素
Memcached::deleteMulti — Delete multiple items
Memcached::deleteMultiByKey — Delete multiple items from a specific server
Memcached::fetch — 抓取下一个结果
Memcached::fetchAll — 抓取所有剩余的结果
Memcached::flush — 作废缓存中的所有元素
Memcached::get — 检索一个元素
Memcached::getAllKeys — Gets the keys stored on all the servers
Memcached::getByKey — 从特定的服务器检索元素
Memcached::getDelayed — 请求多个元素
Memcached::getDelayedByKey — 从指定的服务器上请求多个元素
Memcached::getMulti — 检索多个元素
Memcached::getMultiByKey — 从特定服务器检索多个元素
Memcached::getOption — 获取Memcached的选项值
Memcached::getResultCode — 返回最后一次操作的结果代码
Memcached::getResultMessage — 返回最后一次操作的结果描述消息
Memcached::getServerByKey — 获取一个key所映射的服务器信息
Memcached::getServerList — 获取服务器池中的服务器列表
Memcached::getStats — 获取服务器池的统计信息
Memcached::getVersion — 获取服务器池中所有服务器的版本信息
Memcached::increment — 增加数值元素的值
Memcached::incrementByKey — Increment numeric item's value, stored on a specific server
Memcached::isPersistent — Check if a persitent connection to memcache is being used
Memcached::isPristine — Check if the instance was recently created
Memcached::prepend — 向一个已存在的元素前面追加数据
Memcached::prependByKey — Prepend data to an existing item on a specific server
Memcached::quit — 关闭所有打开的链接。
Memcached::replace — 替换已存在key下的元素
Memcached::replaceByKey — Replace the item under an existing key on a specific server
Memcached::resetServerList — Clears all servers from the server list
Memcached::set — 存储一个元素
Memcached::setByKey — Store an item on a specific server
Memcached::setMulti — 存储多个元素
Memcached::setMultiByKey — Store multiple items on a specific server
Memcached::setOption — 设置一个memcached选项
Memcached::setOptions — Set Memcached options
Memcached::setSaslAuthData — Set the credentials to use for authentication
Memcached::touch — Set a new expiration on an item
Memcached::touchByKey — Set a new expiration on an item on a specific server



add a note

User Contributed Notes 4 notes

up

down

44

joelhy
4 years ago

For those confuse about the memcached extension and the memcache extension, the short story is that both of them are clients of memcached server, and the memcached extension offer more features than the memcache
extension.


up

down

4

gabriel dot maybrun at demandmedia dot com

10 months ago

GOTCHA: Recently I was tasked with moving from PECL memcache to PECL memcached and ran into a major problem -- memcache and memcached serialize data differently, meaning that data written with one library can't
necessarily be read with the other library.

For example, If you write an object or an array with memcache, it's interpreted as an integer by memcached.  If you write it with memcached, it's interpreted as a string by memcache.

tl;dr - You can't safely switch between memcache and memcached without a either a cache flush or isolated cache environments.

<?php

$memcache = new Memcache;

$memcacheD = new
Memcached;

$memcache->addServer($host);

$memcacheD->addServers($servers);

$checks = array(

123,

4542.32,

'a string',

true,

array(123, 'string'),

(object)array('key1' =>
'value1'),

);

foreach ($checks as
$i => $value) {

print "Checking WRITE with Memcache\n";

$key = 'cachetest'
. $i;

$memcache->set($key,
$value);

usleep(100);

$val = $memcache->get($key);

$valD = $memcacheD->get($key);

if ($val !==
$valD) {

print "Not compatible!";

var_dump(compact('val',
'valD'));

}

print "Checking WRITE with MemcacheD\n";

$key = 'cachetest'
. $i;

$memcacheD->set($key,
$value);

usleep(100);

$val = $memcache->get($key);

$valD = $memcacheD->get($key);

if ($val !==
$valD) {

print "Not compatible!";

var_dump(compact('val',
'valD'));

}

}


up

down

1

davidt
2 years ago

The module also supports SASL authentication, it just isn't documented sadly. You'll need to run the following code:

<?php

$m = new Memcached();

$m->setOption(Memcached::OPT_BINARY_PROTOCOL,
true);

$m->setSaslAuthData("user-1",
"pass");

?>

You need to enable the "memcached.use_sasl = 1" ini option for memcached in the php.ini file.


up

down

0

mike at eastghost dot com
3 years ago
It's Feb 2012, and I just spent half a day tracing a weird and sporadic problem back to Memcached extension (vers 1.0.2), which would randomly return SYSTEM ERROR and fail to either get or put data into unix memcached.  Made one single
change from Memcached extension back to Memcache extension and problem instantly vanished.

A year or three ago, I had changed from Memcache to Memcached for some other reason.

Now I'm on a mission to transcend the Memcache/Memcached fiasco.  I'm going to try APC's variable cache, and if that doesn't work better than just revert to using a key:value stash collection in Mongo.

Basically, the Memcache / Memcache driver fiasco, which has been going on apparently for years now, has ruined serious use of unix memcached on PHP...unless you're huge and can afford to create your own MemcacheX driver...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: