Laravel框架中實現使用阿里云ACE緩存服務_PHP教程
推薦:Laravel中擴展Memcached緩存驅動實現使用阿里云OCS緩存這篇文章主要介紹了Laravel中擴展Memcached緩存驅動實現使用阿里云OCS緩存,本文擴展了一個支持SASL 認證模式的Memcached緩存驅動,需要的朋友可以參考下 Laravel 是我最近用得非常多而且越用就越喜歡的一款PHP框架,由于沒有向下兼容的歷史包袱,完全面向對象的風格,借
這篇文章主要介紹了Laravel框架中實現使用阿里云ACE緩存服務,本文擴展了一個ACE緩存驅動,以便使用阿里云ACE緩存服務,需要的朋友可以參考下
之前我寫了一篇在 Laravel 4 框架中使用阿里云 OCS 緩存的文章,介紹了如何通過擴展 Laravel 4 來支持需要 SASL 認證的阿里云 OCS 緩存服務。有網友問我,ACE 的緩存怎么在 Laravel 4 中使用。我本來覺得應該可以完全用相同的辦法,后來自己嘗試的時候才發現,ACE 的緩存差別非常大。所以再寫一篇,介紹一下如何在 Laravel 框架中使用阿里云 ACE 的緩存服務。
如何擴展 Laravel 的緩存驅動
在 Laravel 4 中使用 Cache::get($key), Cache::put($key, $value, $minutes) 這樣的代碼時,實際上是訪問 實例化的 Illuminate\Cache\Repository, 所以我們通過 Cache::extend 方法擴展自定義緩存驅動時,同樣應該返回一個 Illuminate\Cache\Repository 對象。
Laravel 4 內置的 Memcached 緩存驅動,實現的流程是這樣的:
1.創建一個標準 Memcached 類的新對象
2.用上一步創建的 Memcached 對象創建一個實現了 Illuminate\Cache\StoreInterface 接口的 Illuminate\Cache\MemecachedStore 對象。
3.用上一步創建的 MemcachedStore 對象創建一個 Illuminate\Cache\Repository 對象。
所以我們在擴展自定義的 Cache 驅動時,根據自己的情況,選擇上面的某一個步驟自定義,最終還是要返回 Illuminate\Cache\Repository 對象。比如上一篇文章中,我就是在第一步,創建標準 Memcached 對象之后,通過 setSaslAuthData() 方法設定 OCS 需要的用戶名密碼。之后第2步、第3步并不需要自定義。
ACE 的緩存服務
阿里云 ACE 的緩存服務,跟默認的 OCS 有所不同:
1.通過 Alibaba::Cache() 方法獲得 Cache 對象。
2.ACE 的 Cache 對象與標準 Memcached 對象不同,支持的方法有限。
所以,這次第一步得到的不是標準 Memcached 對象,因此就不能創建 Illuminate\Cache\MemcachedStore 對象。需要自己實現 Illuminate\Cache\StoreInterface 接口。
在控制臺創建了緩存空間之后,會有唯一的“緩存空間名稱”,然后通過 Alibaba::Cache('緩存空間名稱') 來獲得 Cache 對象。以下就是實現 ACE 緩存服務驅動的步驟:
1.為了方便修改,我在配置文件 app/config/cache.php 中增加一個名為 ace 的鍵,存儲緩存空間名稱。
2.然后創建一個 AceMemcachedStore 類,這個類實現 Illuminate\Cache\StoreInterface 接口。
3.最后,用 AceMemcachedStore 對象來創建 Illuminate\Cache\Repository 對象。
下面來看具體的代碼實現:
編碼實現自定義 ACE 緩存驅動:
第一步,修改配置文件。打開 app/config/cache.php,在最后增加一行:
代碼如下:
// 指定緩存空間名稱
'ace' => 'lblog-cache',
第二步,為了方便,把自己的類文件放在 src/Ace 目錄下,使用 Ace 作為命名空間。
1.在 app 的同級目錄創建目錄 src/Ace。
2.打開 composer.json 文件,修改 autoload 節,在 classmap 下面用 psr-0 或者 psr-4 來自動加載文件。
代碼如下:
"autoload": {
"classmap": [
// autoload class
],
"psr-4": {
"Ace\\": "src/Ace"
}
},
創建 src/Ace/AceMemcachedStore.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函數mysql_fetch_row、assoc、array、object的區別一、mysql_fetch_row 這個函數是從結果集中取一行作為枚舉數據,從和指定的結果標識關聯的結果集中取得一行數據并作為數組返回。每個結果的列儲存在一個數組的單元中,偏移量從 0 開始。 注意,這里是從0開始偏移,也就是說不能用字段名字來取值,只能用索引來取值。例
- Laravel中擴展Memcached緩存驅動實現使用阿里云OCS緩存
- php函數mysql_fetch_row、assoc、array、object的區別
- include(),require(),include_once(),require_once()的區別
- 基于GD2圖形庫的PHP生成圖片縮略圖類代碼分享
- 支持png透明圖片的php生成縮略圖類分享
- php面向對象中static靜態屬性與方法的內存位置分析
- php中get_object_vars()方法用法實例
- php面向對象中static靜態屬性和靜態方法的調用
- php延遲靜態綁定實例分析
- php定時執行任務設置詳解
- 遷移PHP版本到PHP7
- php include類文件超時問題處理
- 相關鏈接:
- 教程說明:
PHP教程-Laravel框架中實現使用阿里云ACE緩存服務
。