- 浏览: 2509995 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (676)
- linux运维 (157)
- php (65)
- mysql (78)
- nginx (27)
- apche (18)
- framework (6)
- windows (9)
- IDE工具 (23)
- struts2 (7)
- java (13)
- 移动互联网 (14)
- memcache redis (23)
- shell基础/命令/语法 (37)
- shell (50)
- puppet (4)
- C (11)
- python (9)
- 产品经理 (27)
- Sphinx (4)
- svn (12)
- 设计构建 (12)
- 项目管理 (44)
- SEO (1)
- 网站架构 (26)
- 审时度势 (42)
- 网络 (14)
- 激发事业[书&视频] (81)
- 其它 (12)
- 摄影 (8)
- android (21)
最新评论
-
zhongmin2012:
原文的书在哪里
数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器 -
renzhengzhi:
你好,请问个问题,从master同步数据到slave的时候,s ...
数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器 -
ibc789:
你好,看了你的文章,我想请教个问题, 我在用 redis的时候 ...
redis 的两种持久化方式及原理 -
iijjll:
写得非常好
数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器 -
iijjll:
写得非常好
数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器
interface ICache {
public function put($key,$var,$timeout=0);
public function get($key);
public function delete($key);
public function close();
public function open($params);
}
class FileCache{
private $filecache_path;
/**
* 構造函數
* @param String $file DbFlat文件名,不存在將創建
* @param Int $mode DbFlat文件的讀寫模式
* @param Int $perm DbFlat文件權限
*/
function __construct($filePath){
if(file_exists($filePath)){
$this->filecache_path = $filePath;
}else{
$this->make_dir($filePath);
$this->filecache_path = $filePath;
}
}
/**
* 存儲一個Key
* @param String $key Key
* @param String $val Value
* @return Boolean
*/
function store($key,$val){
$files = $this->filecache_path."/{$key}_filecache";
return file_put_contents($files, serialize($val));
}
/**
* 獲得指定Key的值
* @param String $key Key
* @return String
*/
public function fetch($key){
$files = $this->filecache_path."/{$key}_filecache";
if(file_exists($files)){
$str = file_get_contents($files);
if($str === false || $str === '') {
return null;
}
else{
return unserialize($str);
}
}
}
/**
* 刪除一個Key
* @param String $key Key
* @return Boolean
*/
public function delete($key){
$files = $this->filecache_path."/{$key}_filecache";
if(file_exists($files)){
unlink($files);
}
}
/**
* 刪除一個數組里的所以Key
* @param Array $key_array
* @return void
*/
public function delete_array($key_array){
foreach($key_array as $key){
$this->delete($key);
}
}
/**
* 遞迴生成文件夾
* @param str $path 文件夾path, 不是文件的path
* @return mixed 成功時返回創建成功的path,失敗時返回false
*/
public function make_dir($path)
{
if(! file_exists($path))
{
$this->make_dir(dirname($path));
mkdir($path, 0777);
}
return realpath($path);
}
}
require_once('ICache.iface.php');
class ApcCache implements ICache {
public function put($key,$var,$timeout=0) {
$succ = true;//apc_store($key,$var,$timeout);
return $succ;
}
public function get($key) {
//echo "<hr>get==$key==from apc ==<hr>";
//return apc_fetch($key);
return '';
}
public function delete($key) {
//return apc_delete($key);
return '';
}
public function close() { }
public function open($params) { }
}
require_once('ICache.iface.php');
class MemCached implements ICache {
//实例化memcache对象
public $mc = null;
/**
* 构造实例化
* $config_array:memcache服务器IP和端口
*@return obj
*/
public function __construct($config_array){
//服务器端调用方式
$this->mc = new Memcache;
//$mc->connect($memcache_url, 11212); #[单一服务器可用]
if (is_array($config_array)) {
foreach ($config_array as $key=>$value){
$this->mc->addServer($value['IP'], $value['port']);
}
}
$this->mc->setCompressThreshold(10240, 0.2);
/*
$this->mc->setCompressThreshold(10240, 0.2); 此函数在memcache2.0.0加入。
Memcache::setCompressThreshold()开启对于大值的自动压缩。 同样你也可以使用函数memcache_set_compress_threshold()。
参数一:控制多大值进行自动压缩的阈值。
参数二:指定经过压缩实际存储的值的压缩率,支持的值必须在0和1之间。默认值是0.2表示20%压缩率。
$memcache_obj = new Memcache;
$memcache_obj->addServer('memcache_host', 11211);
$memcache_obj->setCompressThreshold(20000, 0.2);
*/
}
/**
* 值加入到memcache
* $key:标识
* $var:值
* $timeout:失效时间 默认时间86400表示为一天时间。
*@return true or false
*/
public function put($key,$var,$timeout=86400,$is_compress=0) {
$succ = $this->mc->set($key,$var,$is_compress,$timeout);
if($succ === false){
//如果失败,休息一秒钟,则继续重写
sleep(1);
$succ = $this->mc->set($key,$var,$is_compress,$timeout);
if($succ === false ){
log_error('set memache key failed!! key:'.$key.' value:'.serialize($var).' timeout:'.$timeout, __FILE__, __LINE__);
}
}
return $succ;
}
/**
* 根据key值从memcache得到值
* $key:标识
*@return string or array
*/
public function get($key) {
$result = $this->mc->get($key);
if($result === false && (is_string($key) && strpos($key,'online') === false)){
//log_error('get memache key ('.$key.') failed!!', __FILE__, __LINE__);
}
return $result;
}
/**
* 根据key值从memcache删除值
* $key:标识
*@return true or false
*/
public function delete($key) {
$succ = $this->mc->delete($key);
if($succ === false){
//如果失败,休息一秒钟,则继续删除
sleep(1);
$succ = $this->mc->delete($key);
//if($succ === false){
// log_error('delete memache key('.$key.') failed!!',__FILE__,__LINE__);
//}
}
return $succ;
}
/**
* 关键memcache
*@return true or false
*/
public function close(){
return $this->mc->close();
}
public function open($params) { }
}
require_once('ICache.iface.php');
require_once('LocalCache.class.php');
class CacheStore{
private $_caches;
private $_storeName;
private $_defaultCache;
public function setName($name){
$this->_storeName = $name;
}
public function setCache(ICache $cache, $privilege=0){
$this->_caches[$privilege] = $cache;
}
public function getCache($privilege=0){
return $this->_caches[$privilege];
}
public function init(){
//if(!is_array($this->_caches)){
$this->setCache(new LocalCache());
//}
ksort($this->_caches);
}
public function put($key, $value, $privilege=-1){
if($privilege == 0){ //指定赋值给默认的
$this->_caches[0]->put($this->_storeName . '_' . $key, $value);
}else{
foreach($this->_caches as $cache){
$cache->put($this->_storeName . '_' . $key, $value);
}
}
}
public function get($key){
$i = 0;
foreach($this->_caches as $cache){
$val = null;
$val = $cache->get($this->_storeName . '_' . $key);
if($val!=null){
if($i > 0){
$this->put($key,$val,0);
}
return $val;
}
$i++;
}
return $val;
}
}
$arr_server_memcache = array(
array(
'IP' => '192.168.101.73',
'port' => '12000'
),
array(
'IP' => '192.168.101.74',
'port' => '12000'
),
array(
'IP' => '192.168.101.75',
'port' => '12000'
),
array(
'IP' => '192.168.101.76',
'port' => '12000'
),
array(
'IP' => '192.168.101.77',
'port' => '12000'
)
);
require('pworks/util/cache/CacheStore.class.php');
require_once('pworks/util/cache/ApcCache.class.php');
require_once('pworks/util/cache/MemCache.class.php');
$appCache = new CacheStore();
$appCache->setName(MARKSIX_SYSTEM.'_mvc_app_obj');
//$appCache->setCache(new ApcCache());
//加入memcached缓存
if (MEMCACHE) {
$appCache->setCache(new MemCached($arr_server_memcache), 'mc');
}
$appCache->init();
/**
* 得到memcache实例
*/
function memc(){
return FrontController::getCache('mc');
}
/**
* 得到memcache里存放当前用户标识的key
*/
function get_mem_key(){
return COM_TYPE == 'f' ? get_online_member_key($_SESSION['memberId']) : get_online_user_key($_SESSION['userId']);
}
/**
* 给memcache里存放当前用户标识的key赋值当前的session,EXPIRE_TIME默认为 10
*/
function set_mem_key(){
return memc()->put
(get_mem_key(), get_new_session_id(get_session_id()), EXPIRE_TIME);
}
发表评论
-
通过Keepalived实现Redis Failover自动故障切换功能[实践分享]
2015-03-12 17:10 7371参考资料:http://patrick-tang.blog ... -
Redis学习手册(主从复制)
2015-03-12 17:05 7311一、Redis的Replication: ... -
redis 的两种持久化方式及原理
2015-03-12 16:52 63220Redis是一种高级key-value数据库。它跟memc ... -
Redis指令手册中文版
2014-08-08 17:09 915连接控制QUIT :退出,关 ... -
Redis服务的监控
2013-01-17 16:00 6645redis 监控工具安装配置http://blog.sin ... -
【汇总】redis
2012-04-16 13:51 1414http://bbs.chinaunix.net/thread ... -
memcache 丢数据解决方案
2011-07-01 11:17 2661一小时以内数据变化都存放到memcached中,无论数据读取或 ... -
Memcached repcached同步
2011-06-10 14:10 2152#安装 wget http://downloads.sour ... -
memcached命中率问题
2011-06-09 21:14 1972http://www.iteye.com/topic/2256 ... -
php遍历memcache所有键值
2011-05-31 17:56 4336很有用的东东,PHP遍历MEMCACHE的所有键,在做管理me ... -
memcached全面剖析和深度剖析
2011-05-15 14:43 1644Memcached深度分析(原创) 2009年2月11日 ... -
【监控】netstat+telnet+m-top+phpmemcached
2011-03-31 22:01 29921G=1024*1024*1024=1073741824 by ... -
centos 下安装memcache配置
2010-09-21 15:01 7408为什么会有Memcache和memca ... -
关于session和memcache的若干问题
2010-09-20 15:17 5555=============================== ... -
Memcached深度分析----转新浪开发者博客
2009-07-11 22:36 1635http://blog.developers.api.sina ... -
多台memcache 研究
2009-07-11 13:07 4814第一种方法是通过设置特殊key前缀实现分布,基本可以实现mem ... -
Memcache分组和同步机制的实现--这不是memcache集群了么
2009-07-05 21:53 13018首先我们明确了解就是Memcache是一个简单、快速、高效的分 ... -
memcached全面剖析–5. memcached的应用和兼容程序
2009-07-05 21:31 1567memcached的连载终于要结束了。到上次为止,我们介绍了与 ... -
[推荐]memcached全面剖析–4. memcached的分布式算法:Consistent Hashing
2009-07-05 21:20 2483第2次 、 第3次 由前坂介绍了memcached的内部情况 ... -
【汇总】Memcache
2009-07-01 11:40 3660memcached全面剖析和深度剖析.rar ...
相关推荐
C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档C语言memcache代码文档
memcache配置源代码
C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码
C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码C语言memcache代码
最为高效的java遍历Memcache详细代码
memcached 源代码分析,出自社区大咖,货真价实,值得信赖。
memcache 范例 附源代码 缓存 超级不错的缓存解决方案
memcache也有一款图形界面的监控工具(memcachephp),可以通过这个工具查看到局域网内所有部署memcache机器或者端口的memcache的运行情况,对我们监控memcache的缓存命中率、cache利用率、点击率等信息有很好的帮助...
memcache图形监控工具phpmemcache,尽是一个PHP文件就可以实现对memcache的监控。 使用方法:本地测试监控机安装Apache或者下载XAMPP(Apache+MySQL+PHP+PERL),安装后把memcachephp.zip中的memcache.php文件放到...
windows下memcache安装包 附带php扩展包
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问。
memcached的源代码.zip
测试代码:注意端口的匹配 $memcache = new Memcache; $memcache->connect("localhost",11211); echo "Server's version: " . $memcache->getVersion() . "\n"; $tmp_object = new stdClass; $tmp_object->str_attr ...
Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从...
最新windows的memcache模块下载 这个模块是平和php5.3的,在我的windowsxp php5.3.5上安装成功 里面有两个php库,一个php_memcache.dll.vc6 和一个php_memcache.dll.vc9 另外一个windows的memcache.exe文件,都是网上...
将信息保持memcache中
Memcache就不用多介绍了,做开发的人都知道。 但要用得好,却并不是那么容易的事。 如果用得不好,反而得不偿失。 这篇文档短小精悍,囊括了使用过程中需要要注意的方方面面。值得一读。
MemCache.0.2.0.zip Memcached Client for Delphi 客户端调用类 MemCache.0.2.0.zip Show all LinksExternal links Memcached Project This project is a delphi unit which implements a thread safe client for ...
简单的memcache命令
windows memcache 安装服务,php_memcache.dll所有版本扩展dll 安装说明 在命令行下安装Memcache,输入 ‘c:/memcached/memcached.exe -d install’。 3.启动Memcache,再输入: ‘c:/memcached/memcached.exe -d ...