// 指定缓存空间名称 ‘ace‘ => ‘lblog-cache‘,
"autoload": {
"classmap": [
// autoload class
],
"psr-4": {
"Ace\\": "src/Ace"
}
},
<?php
namespace Ace;
use Illuminate\Cache\StoreInterface;
use Illuminate\Cache\TaggableStore;
class AceMemcachedStore extends TaggableStore implements StoreInterface {
protected $memcached;
protected $prefix;
public function __construct($space, $prefix = ‘‘) {
$this->memcached = \Alibaba::Cache($space);
$this->prefix = strlen($prefix) > 0 ? $prefix.‘:‘ : ‘‘;
}
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = $this->memcached->get($this->prefix.$key);
if(is_bool($value) && $value === false) {
return null;
}
return $value;
}
/**
* Store an item in the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return boolean
*/
public function put($key, $value, $minutes)
{
return $this->memcached->set($this->prefix.$key, $value, $minutes);
}
/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function increment($key, $value = 1)
{
return $this->memcached->increment($this->prefix.$key, $value);
}
/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function decrement($key, $value = 1)
{
return $this->memcached->decrement($this->prefix.$key, $value);
}
/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function forever($key, $value)
{
return $this->memcached->set($key, $value, 0);
}
/**
* Remove an item from the cache.
*
* @param string $key
* @return boolean
*/
public function forget($key)
{
return $this->memcached->delete($this->prefix.$key);
}
/**
* Remove all items from the cache.
*
* @return void
*/
public function flush()
{
//$this->memcached->flush();
return false;
}
public function getMemcached()
{
return $this->memcached;
}
/**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
}
// 扩展名为 ace 的缓存驱动
Cache::extend(‘ace‘, function($app)
{
// 从 app/config/cache.php 文件中读取 "ace" 的值
$space = $app[‘config‘][‘cache.ace‘];
// 从 app/config/cache.php 文件中读取 "prefix" 的值
$prefix = $app[‘config‘][‘cache.prefix‘];
// 创建 \Ace\AceMemcachedStore 对象
$store = new \Ace\AceMemcachedStore($space, $prefix);
// 创建并返回 \Illuminate\Cache\Repository 对象
return new \Illuminate\Cache\Repository($store);
});
// 添加缓存,有效时间10分钟 Cache::put(‘my_key‘, ‘my value‘, 10); // 读取缓存 Cache::get(‘my_key‘) // 判断缓存是否存在 Cache::has(‘my_key‘) // 数据查询缓存 $users = DB::table(‘users‘)->remember(10)->get();
19Laravel框架学习笔记(二)项目实战之模型(Models)
25推荐几款用 Sublime Text 开发 Laravel 所用到的插件
26Laravel 4.2 中队列服务(queue)使用感受
36Laravel中使用阿里云OSS Composer包分享
37Laravel模板引擎Blade中section的一些标签的区别介绍
50PHP IDE PHPStorm配置支持友好Laravel代码提示方法
51Sublime Text 插件推荐系列 —— 用 Sublime 开发 Laravel
原文地址:http://blog.csdn.net/u011252402/article/details/46922527