1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace Common\Cache;
- use Memcached;
- /**
- * 加载redis
- * @539 2016-4-6
- */
- class Mecached {
- protected $mcache;
- public static $_MCaches = array();
- public function __construct() {
- if (!extension_loaded('memcached')) {
- throw_exception(L('_NOT_SUPPERT_') . ':memcached');
- }
- $this->initCache(MC_HOST);
- }
- public function initCache($cache_server_name){
- if(isset(self::$_MCaches[$cache_server_name])){
- $this->mcache = self::$_MCaches[$cache_server_name];
- }else{
- self::$_MCaches[$cache_server_name] = new Memcached; //声明一个新的memcached链接
- self::$_MCaches[$cache_server_name]->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能
- self::$_MCaches[$cache_server_name]->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //使用binary二进制协议
- self::$_MCaches[$cache_server_name]->addServer($cache_server_name, MC_PORT);
- $this->mcache = self::$_MCaches[$cache_server_name];
- }
-
- }
- public function get($key) { //获取MC值
- if (empty($key)) {
- return false;
- }
- $res = $this->mcache->get($key);
- return $res;
- }
- /**
- * @param type $key key值
- * @param type $val 存入的值
- * @param type $expire 过期时间,默认null秒 //设置为0表明此数据永不过期(但是它可能由于服务端为了给其他新的元素分配空间而被删除)
- * @return boolean
- */
- public function set($key, $val, $expire = null) {
- if (empty($key)) {
- return false;
- }
- $res = $this->mcache->set($key, $val, $expire);
- return $res;
- }
- public function saveOne($key, $update_val, $expire = null) {
- $val = $this->mcache->get($key);
- if (is_array($val)) {
- $update_val = array_merge($val, $update_val);
- }
- $res = $this->mcache->set($key, $update_val, $expire);
- return $res;
- }
- public function delete($key) { //根据key清除缓存
- if (empty($key)) {
- return false;
- }
- $res = $this->mcache->delete($key);
- return $res;
- }
- public function getMulti() {
- }
- public function setMulti() {
- }
- public function delMulti() {
- }
- //自增
- public function increment($key,$val){
- $res = $this->mcache->increment($key, $val);
- return $res;
- }
- public function clearAll() { //清除所有缓存
- $res = $this->mcache->flush();
- return $res;
- }
- }
|