《PHP學(xué)習(xí):Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP編程之前我寫了一篇在 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ù).
PHP編程如何擴(kuò)展 Laravel 的緩存驅(qū)動(dòng)
PHP編程在 Laravel 4 中使用 Cache::get($key), Cache::put($key, $value, $minutes) 這樣的代碼時(shí),實(shí)際上是拜訪 實(shí)例化的 Illuminate\Cache\Repository, 所以我們通過 Cache::extend 方法擴(kuò)展自定義緩存驅(qū)動(dòng)時(shí),同樣應(yīng)該返回一個(gè) Illuminate\Cache\Repository 對(duì)象.
PHP編程Laravel 4 內(nèi)置的 Memcached 緩存驅(qū)動(dòng),實(shí)現(xiàn)的流程是這樣的:
PHP編程1.創(chuàng)建一個(gè)標(biāo)準(zhǔn) Memcached 類的新對(duì)象
2.用上一步創(chuàng)建的 Memcached 對(duì)象創(chuàng)建一個(gè)實(shí)現(xiàn)了 Illuminate\Cache\StoreInterface 接口的 Illuminate\Cache\MemecachedStore 對(duì)象.
3.用上一步創(chuàng)建的 MemcachedStore 對(duì)象創(chuàng)建一個(gè) Illuminate\Cache\Repository 對(duì)象.
PHP編程所以我們?cè)跀U(kuò)展自定義的 Cache 驅(qū)動(dòng)時(shí),根據(jù)自己的情況,選擇上面的某一個(gè)步驟自定義,最終還是要返回 Illuminate\Cache\Repository 對(duì)象.比如上一篇文章中,我就是在第一步,創(chuàng)建標(biāo)準(zhǔn) Memcached 對(duì)象之后,通過 setSaslAuthData() 方法設(shè)定 OCS 需要的用戶名暗碼.之后第2步、第3步并不需要自定義.
PHP編程ACE 的緩存服務(wù)
PHP編程阿里云 ACE 的緩存服務(wù),跟默認(rèn)的 OCS 有所不同:
PHP編程1.通過 Alibaba::Cache() 辦法獲得 Cache 對(duì)象.
2.ACE 的 Cache 對(duì)象與標(biāo)準(zhǔn) Memcached 對(duì)象不同,支持的辦法有限.
PHP編程所以,這次第一步得到的不是標(biāo)準(zhǔn) Memcached 對(duì)象,因此就不能創(chuàng)建 Illuminate\Cache\MemcachedStore 對(duì)象.需要自己實(shí)現(xiàn) Illuminate\Cache\StoreInterface 接口.
PHP編程在控制臺(tái)創(chuàng)建了緩存空間之后,會(huì)有唯一的“緩存空間名稱”,然后通過 Alibaba::Cache('緩存空間名稱') 來獲得 Cache 對(duì)象.以下就是實(shí)現(xiàn) ACE 緩存服務(wù)驅(qū)動(dòng)的步驟:
PHP編程1.為了方便修改,我在配置文件 app/config/cache.php 中增加一個(gè)名為 ace 的鍵,存儲(chǔ)緩存空間名稱.
2.然后創(chuàng)建一個(gè) AceMemcachedStore 類,這個(gè)類實(shí)現(xiàn) Illuminate\Cache\StoreInterface 接口.
3.最后,用 AceMemcachedStore 對(duì)象來創(chuàng)建 Illuminate\Cache\Repository 對(duì)象.
PHP編程下面來看具體的代碼實(shí)現(xiàn):
PHP編程編碼實(shí)現(xiàn)自定義 ACE 緩存驅(qū)動(dòng):
PHP編程第一步,修改配置文件.打開 app/config/cache.php,在最后增加一行:
代碼如下:
// 指定緩存空間名稱
'ace' => 'lblog-cache',
第二步,為了方便,把自己的類文件放在 src/Ace 目錄下,使用 Ace 作為命名空間.
PHP編程1.在 app 的同級(jí)目錄創(chuàng)建目錄 src/Ace.
2.打開 composer.json 文件,修改 autoload 節(jié),在 classmap 下面用 psr-0 或者 psr-4 來自動(dòng)加載文件.
代碼如下:
"autoload": {
??? "classmap": [
??????? // autoload class
??? ],
??? "psr-4": {
????? "Ace\\": "src/Ace"
??? }
},
PHP編程創(chuàng)建 src/Ace/AceMemcachedStore.php 文件,代碼如下:
代碼如下:
<?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;
??? }
}
PHP編程這段代碼比較簡(jiǎn)單,不過要特別注意一下 get($key) 辦法的實(shí)現(xiàn).標(biāo)準(zhǔn) memcached 以及 ACE 的緩存對(duì)象的 get 辦法都是key有效時(shí)返回對(duì)應(yīng)的緩存值,否則返回false,而在 Laravel 4 中,是通過檢測(cè) get 辦法返回的是否 null 來做判斷,所以這里需要處理一下,返回緩存值或者null.
PHP編程AceMemcachedStore類已經(jīng)創(chuàng)建好了,接下來在 bootstrap/start.php 文件中擴(kuò)展 Cache:
PHP編程打開 bootstrap/start.php, 添加以下代碼:
代碼如下:
// 擴(kuò)展名為 ace 的緩存驅(qū)動(dòng)
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)建 \Ace\AceMemcachedStore 對(duì)象
??? $store = new \Ace\AceMemcachedStore($space, $prefix);
????
??? // 創(chuàng)建并返回 \Illuminate\Cache\Repository 對(duì)象
??? return new \Illuminate\Cache\Repository($store);
?
});
PHP編程指定系統(tǒng)使用 'ace' 作為緩存驅(qū)動(dòng):打開 app/config/cache.php,找到 'driver' => '...' 所在行,修改為:'driver' => 'ace'.
PHP編程使用和限制
PHP編程通過以上操作,就可以在 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();
PHP編程但是由于 ACE 緩存對(duì)象本身的限制,只能刪除指定 key 的緩存對(duì)象,不能遍歷、全量操作,因此 Cache::flush() 辦法就不能使用.在上面的 AceMemcachedStore 對(duì)象中,flush 辦法沒有做任何操作,只是返回 false.
歡迎參與《PHP學(xué)習(xí):Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/12428.html