在Windows下使用Memcache扩展模拟Memcached

众所周知,在windows下PHP是无法使用Memcached扩展的,只能使用Memcache(少一个d),来请求Memcached服务。而一些PHP框架(如:Laravel)使用的是Memcached扩展,造成在windows无法使用Memcache的相关功能。在不改动框架代码的情况下如何能让程序正常运行?

那就自己动手写一个Memcached吧,用Memcache来模拟一下。

新建一个PHP文件:windows_memcached.php

<?php
class Memcached{
    private $persistent_id;
	private $mem;
	private $servers = array();

	public function __construct($persistent_id=null){
		$this->mem = new Memcache();
		$this->persistent_id = $persistent_id;
	}

	public function add($key, $value, $expiration){
		return $this->mem->add($key, $value, false, $expiration);
	}

	public function addByKey($server_key, $key, $value, $expiration){
		return $this->add($key, $value, $expiration);
	}

	public function addServer ($host, $port, $weight=0){
		$this->servers[] = array($host, $port, $weight=0);
		return $this->mem->addServer ($host, $port, $weight);
	}

	public function addServers (array $servers){
		foreach($servers as $server){
			$flag = $this->mem->addServer($server[0], $server[1], $server[2]);
			if(!$flag){
				throw new \Exception($server[0].':'.$server[1].' '.$server[2].' add error');
			}
		}
		return true;
	}

	public function append ($key, $value){
		$get = $this->mem->get($key);
		if(false !== $get && is_string($get)){
			$value = $get.$value;
		}

		return $this->mem->set($key, $value);
	}

	public function appendByKey ($server_key, $key, $value ){
		return $this->append($key, $value);
	}

	public function getServerList(){
		return $this->servers;
	}

	public function getMulti(){ return array();}

	public function get($key, $cache_cb, $cas_token){
		return $this->mem->get($key);
	}
	public function set($key, $value, $expiration){
		return $this->mem->set($key, $value, false, $expiration);
	}
	public function replace($key, $value, $expiration){
		return $this->mem->replace($key , $value, false, $expiration);
	}
	public function increment($key, $offset = 1){
		return $this->mem->increment($key, $offset);
	}
	public function decrement($key, $offset = 1){
		return $this->mem->decrement($key, $offset);
	}
	public function delete($key, $time = 0){
		return $this->mem->delete($key, $time);
	}
	public function flush($delay = 0){
		return $this->mem->flush();
	}

	public function quit(){
		return $this->mem->close();
	}
}

这样,将使用到的方法放到类里之后,还要做关键的一步:加载这个类。

不改动框架或项目的代码来实现加载要用到php.ini中的配置项:auto_prepend_file(这个参数在php -r 方式下运行会被忽略[参考])

打开php.ini找到这个配置项,添加文件路径:

auto_prepend_file = "D:\php\windows_ext\windows_memcached.php"

重启下PHP的服务,使配置生效。

 

写个测试:a.php

<?php
$m = new Memcached();
print_r($m);

运行看下结果:php a.php

Memcached Object
(
    [persistent_id:Memcached:private] =>
    [mem:Memcached:private] => Memcache Object
        (
        )

    [servers:Memcached:private] => Array
        (
        )

)

可以正常调用,OK, well done!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值