国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

Laravel框架如何使用阿里云ACE緩存服務(wù)

2020-03-22 19:19:10
字體:
供稿:網(wǎng)友
  • 之前我寫了一篇在 Laravel 4 框架中使用阿里云 OCS 緩存的文章,介紹了如何通過擴(kuò)展 Laravel 4 來支持需要 SASL 認(rèn)證的阿里云 OCS 緩存服務(wù)。有網(wǎng)友問我,ACE 的緩存怎么在 Laravel 4 中使用。我本來覺得應(yīng)該可以完全用相同的辦法,后來自己嘗試的時(shí)候才發(fā)現(xiàn),ACE 的緩存差別非常大。所以再寫一篇,介紹一下如何在 Laravel 框架中使用阿里云 ACE 的緩存服務(wù)。

    如何擴(kuò)展 Laravel 的緩存驅(qū)動

    在 Laravel 4 中使用 Cache::get($key), Cache::put($key, $html' target='_blank'>value, $minutes) 這樣的代碼時(shí),實(shí)際上是訪問 實(shí)例化的 IlluminateCacheRepository, 所以我們通過 Cache::extend 方法擴(kuò)展自定義緩存驅(qū)動時(shí),同樣應(yīng)該返回一個(gè) IlluminateCacheRepository 對象。

    Laravel 4 內(nèi)置的 Memcached 緩存驅(qū)動,實(shí)現(xiàn)的流程是這樣的:

    1.創(chuàng)建一個(gè)標(biāo)準(zhǔn) Memcached 類的新對象

    2.用上一步創(chuàng)建的 Memcached 對象創(chuàng)建一個(gè)實(shí)現(xiàn)了 IlluminateCacheStoreInterface 接口的 IlluminateCacheMemecachedStore 對象。

    3.用上一步創(chuàng)建的 MemcachedStore 對象創(chuàng)建一個(gè) IlluminateCacheRepository 對象。
    所以我們在擴(kuò)展自定義的 Cache 驅(qū)動時(shí),根據(jù)自己的情況,選擇上面的某一個(gè)步驟自定義,最終還是要返回 IlluminateCacheRepository 對象。比如上一篇文章中,我就是在第一步,創(chuàng)建標(biāo)準(zhǔn) Memcached 對象之后,通過 setSaslAuthData() 方法設(shè)定 OCS 需要的用戶名密碼。之后第2步、第3步并不需要自定義。

    ACE 的緩存服務(wù)

    阿里云 ACE 的緩存服務(wù),跟默認(rèn)的 OCS 有所不同:

    1.通過 Alibaba::Cache() 方法獲得 Cache 對象。

    2.ACE 的 Cache 對象與標(biāo)準(zhǔn) Memcached 對象不同,支持的方法有限。
    所以,這次第一步得到的不是標(biāo)準(zhǔn) Memcached 對象,因此就不能創(chuàng)建 IlluminateCacheMemcachedStore 對象。需要自己實(shí)現(xiàn) IlluminateCacheStoreInterface 接口。

    控制臺創(chuàng)建了緩存空間之后,會有唯一的“緩存空間名稱”,然后通過 Alibaba::Cache('緩存空間名稱') 來獲得 Cache 對象。以下就是實(shí)現(xiàn) ACE 緩存服務(wù)驅(qū)動的步驟:

    1.為了方便修改,我在配置文件 app/config/cache.php 中增加一個(gè)名為 ace 的鍵,存儲緩存空間名稱。

    2.然后創(chuàng)建一個(gè) AceMemcachedStore 類,這個(gè)類實(shí)現(xiàn) IlluminateCacheStoreInterface 接口。

    3.最后,用 AceMemcachedStore 對象來創(chuàng)建 IlluminateCacheRepository 對象。
    下面來看具體的代碼實(shí)現(xiàn):

    編碼實(shí)現(xiàn)自定義 ACE 緩存驅(qū)動:

    第一步,修改配置文件。打開 app/config/cache.php,在最后增加一行:

    // 指定緩存空間名稱'ace' => 'lblog-cache',


    第二步,為了方便,把自己的類文件放在 src/Ace 目錄下,使用 Ace 作為命名空間。
    1.在 app 的同級目錄創(chuàng)建目錄 src/Ace。

    2.打開 composer.json 文件,修改 autoload 節(jié),在 classmap 下面用 psr-0 或者 psr-4 來自動加載文件。

    "autoload": {    "classmap": [        // autoload class    ],    "psr-4": {      "Ace/": "src/Ace"    }},

    創(chuàng)建 src/Ace/AceMemcachedStore.php 文件,代碼如下:

    <?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;    }}

    這段代碼比較簡單,不過要特別注意一下 get($key) 方法的實(shí)現(xiàn)。標(biāo)準(zhǔn) memcached 以及 ACE 的緩存對象的 get 方法都是key有效時(shí)返回對應(yīng)的緩存值,否則返回false,而在 Laravel 4 中,是通過檢測 get 方法返回的是否 null 來做判斷,所以這里需要處理一下,返回緩存值或者null。

    AceMemcachedStore類已經(jīng)創(chuàng)建好了,接下來在 bootstrap/start.php 文件中擴(kuò)展 Cache:http://www.lai18.com/content/368670.html

    打開 bootstrap/start.php, 添加以下代碼:

    // 擴(kuò)展名為 ace 的緩存驅(qū)動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'];         // 創(chuàng)建 AceAceMemcachedStore 對象    $store = new AceAceMemcachedStore($space, $prefix);         // 創(chuàng)建并返回 IlluminateCacheRepository 對象    return new IlluminateCacheRepository($store); });

    指定系統(tǒng)使用 'ace' 作為緩存驅(qū)動:打開 app/config/cache.php,找到 'driver' => '...' 所在行,修改為:'driver' => 'ace'.

    使用和限制

    通過以上操作,就可以在 Laravel 4 中調(diào)用 ACE 的緩存服務(wù),使用上與平常的用法完全一致,比如:

    // 添加緩存,有效時(shí)間10分鐘Cache::put('my_key', 'my value', 10); // 讀取緩存Cache::get('my_key') // 判斷緩存是否存在Cache::has('my_key') // 數(shù)據(jù)查詢緩存$users = DB::table('users')->remember(10)->get();

    但是由于 ACE 緩存對象本身的限制,只能刪除指定 key 的緩存對象,不能遍歷、全量操作,因此 Cache::flush() 方法就不能使用。在上面的 AceMemcachedStore 對象中,flush 方法沒有做任何操作,只是返回 false.

    PHP編程

    鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 红河县| 枣庄市| 宁南县| 布拖县| 莱阳市| 呼伦贝尔市| 英超| 高邮市| 佛山市| 阿巴嘎旗| 泽州县| 台东县| 罗甸县| 当阳市| 家居| 平邑县| 东港市| 滦南县| 临清市| 清河县| 张北县| 潞城市| 卢湾区| 彭泽县| 新宁县| 独山县| 天峨县| 呼和浩特市| 叶城县| 湘西| 循化| 昌宁县| 岐山县| 临高县| 乌海市| 日喀则市| 合肥市| 阿坝县| 酒泉市| 靖宇县| 米脂县|