| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace PhpLife\Frame\Package\Cache;
- use PhpLife\Frame\Request;
- /**
- * 使用Memcache做缓存
- */
- class Memcache extends \PhpLife\Frame\Package\Cache
- {
- const CONFIG_CLASS = 'Memcache\\Core';
- const PREFIX = '';
- const EXPIRE = 259200;
- /**
- * 单个读取
- * @param $id
- * @return array
- */
- public static function get($id)
- {
- if(Request::isCacheEnabled() == false){
- return array();
- }
- $key = self::getKey($id);
- $result = self::getConnection()->get($key);
- return $result;
- }
- /**
- * 批量读取,如果没有数据,则不在返回的列表中
- * @param array $idList
- * @return array
- */
- public static function getBatch(Array $idList)
- {
- if(Request::isCacheEnabled() == false){
- return array();
- }
- $keyList = array();
- foreach ($idList as $id) {
- $keyList[$id] = self::getKey($id);
- }
- $result = self::getConnection()->getMulti($keyList);
- $data = array();
- if ($result) {
- $keyList = array_flip($keyList);
- foreach ($result as $k => $v) {
- $k = $keyList[$k];
- $data[$k] = $v;
- }
- }
- unset($result);
- unset($idList);
- unset($keyList);
- return $data;
- }
- /**
- * @param $id
- * @param $data
- * @param expire
- * @return bool
- */
- public static function set($id, $data, $expire = null)
- {
- $key = self::getKey($id);
- $class = get_called_class();
- if(is_null($expire)){
- $expire = $class::EXPIRE;
- }
- $expire = time() + intval($expire);
- $result = self::getConnection()->set($key, $data, $expire);
- return $result;
- }
- /**
- * 批量设置
- * @param array $data
- * @param $expire
- * @return bool
- */
- public static function setBatch($data, $expire = null)
- {
- $dataList = array();
- foreach ($data as $k => $v) {
- $k = self::getKey($k);
- $dataList[$k] = $v;
- }
- $class = get_called_class();
- if(is_null($expire)){
- $expire = $class::EXPIRE;
- }
- $expire = time() + intval($expire);
- $result = self::getConnection()->setMulti($dataList, $expire);
- unset($data);
- unset($dataList);
- return $result;
- }
- /**
- * 增加
- * @param $id
- * @param int $value
- * @return int
- */
- public static function increase($id, $value = 1)
- {
- $key = self::getKey($id);
- $value = abs($value);
- return self::getConnection()->increment($key, $value);
- }
- /**
- * 减少
- * @param $id
- * @param int $value
- * @return int
- */
- public static function decrease($id, $value = 1)
- {
- $key = self::getKey($id);
- $value = abs($value);
- return self::getConnection()->decrement($key, $value);
- }
- /**
- * 删除
- * @param $id
- * @return bool
- */
- public static function delete($id)
- {
- $key = self::getKey($id);
- return self::getConnection()->delete($key);
- }
- /**
- * 获取连接
- * @return \Memcached
- */
- protected static function getConnection()
- {
- $class = get_called_class();
- $configClass = $class::CONFIG_CLASS;
- $memcached = new \PhpLife\Frame\Library\Memcache($configClass);
- $connection = $memcached->getConnection();
- return $connection;
- }
- }
|