// 指定緩存空間名稱'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();
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答