// 指定缓存空间名称'ace' => 'lblog-cache',
"autoload": { "classmap": [ // autoload class ], "psr-4": { "Ace/": "src/Ace" }},
<?php namespace Ace;use IlluminateCacheStoreInterface;use IlluminateCacheTaggableStore; 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']; // 创建 AceAceMemcachedStore 对象 $store = new AceAceMemcachedStore($space, $prefix); // 创建并返回 IlluminateCacheRepository 对象 return new IlluminateCacheRepository($store); });
// 添加缓存,有效时间10分钟Cache::put('my_key', 'my value', 10); // 读取缓存Cache::get('my_key') // 判断缓存是否存在Cache::has('my_key') // 数据查询缓存$users = DB::table('users')->remember(10)->get();
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答