Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)_PHP教程
推薦:Laravel中擴(kuò)展Memcached緩存驅(qū)動實(shí)現(xiàn)使用阿里云OCS緩存這篇文章主要介紹了Laravel中擴(kuò)展Memcached緩存驅(qū)動實(shí)現(xiàn)使用阿里云OCS緩存,本文擴(kuò)展了一個支持SASL 認(rèn)證模式的Memcached緩存驅(qū)動,需要的朋友可以參考下 Laravel 是我最近用得非常多而且越用就越喜歡的一款PHP框架,由于沒有向下兼容的歷史包袱,完全面向?qū)ο蟮娘L(fēng)格,借
這篇文章主要介紹了Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù),本文擴(kuò)展了一個ACE緩存驅(qū)動,以便使用阿里云ACE緩存服務(wù),需要的朋友可以參考下
之前我寫了一篇在 Laravel 4 框架中使用阿里云 OCS 緩存的文章,介紹了如何通過擴(kuò)展 Laravel 4 來支持需要 SASL 認(rèn)證的阿里云 OCS 緩存服務(wù)。有網(wǎng)友問我,ACE 的緩存怎么在 Laravel 4 中使用。我本來覺得應(yīng)該可以完全用相同的辦法,后來自己嘗試的時候才發(fā)現(xiàn),ACE 的緩存差別非常大。所以再寫一篇,介紹一下如何在 Laravel 框架中使用阿里云 ACE 的緩存服務(wù)。
如何擴(kuò)展 Laravel 的緩存驅(qū)動
在 Laravel 4 中使用 Cache::get($key), Cache::put($key, $value, $minutes) 這樣的代碼時,實(shí)際上是訪問 實(shí)例化的 Illuminate\Cache\Repository, 所以我們通過 Cache::extend 方法擴(kuò)展自定義緩存驅(qū)動時,同樣應(yīng)該返回一個 Illuminate\Cache\Repository 對象。
Laravel 4 內(nèi)置的 Memcached 緩存驅(qū)動,實(shí)現(xiàn)的流程是這樣的:
1.創(chuàng)建一個標(biāo)準(zhǔn) Memcached 類的新對象
2.用上一步創(chuàng)建的 Memcached 對象創(chuàng)建一個實(shí)現(xiàn)了 Illuminate\Cache\StoreInterface 接口的 Illuminate\Cache\MemecachedStore 對象。
3.用上一步創(chuàng)建的 MemcachedStore 對象創(chuàng)建一個 Illuminate\Cache\Repository 對象。
所以我們在擴(kuò)展自定義的 Cache 驅(qū)動時,根據(jù)自己的情況,選擇上面的某一個步驟自定義,最終還是要返回 Illuminate\Cache\Repository 對象。比如上一篇文章中,我就是在第一步,創(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)建 Illuminate\Cache\MemcachedStore 對象。需要自己實(shí)現(xiàn) Illuminate\Cache\StoreInterface 接口。
在控制臺創(chuàng)建了緩存空間之后,會有唯一的“緩存空間名稱”,然后通過 Alibaba::Cache('緩存空間名稱') 來獲得 Cache 對象。以下就是實(shí)現(xiàn) ACE 緩存服務(wù)驅(qū)動的步驟:
1.為了方便修改,我在配置文件 app/config/cache.php 中增加一個名為 ace 的鍵,存儲緩存空間名稱。
2.然后創(chuàng)建一個 AceMemcachedStore 類,這個類實(shí)現(xiàn) Illuminate\Cache\StoreInterface 接口。
3.最后,用 AceMemcachedStore 對象來創(chuàng)建 Illuminate\Cache\Repository 對象。
下面來看具體的代碼實(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 文件,代碼如下:
代碼如下:
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函數(shù)mysql_fetch_row、assoc、array、object的區(qū)別一、mysql_fetch_row 這個函數(shù)是從結(jié)果集中取一行作為枚舉數(shù)據(jù),從和指定的結(jié)果標(biāo)識關(guān)聯(lián)的結(jié)果集中取得一行數(shù)據(jù)并作為數(shù)組返回。每個結(jié)果的列儲存在一個數(shù)組的單元中,偏移量從 0 開始。 注意,這里是從0開始偏移,也就是說不能用字段名字來取值,只能用索引來取值。例
- Laravel中擴(kuò)展Memcached緩存驅(qū)動實(shí)現(xiàn)使用阿里云OCS緩存
- php函數(shù)mysql_fetch_row、assoc、array、object的區(qū)別
- include(),require(),include_once(),require_once()的區(qū)別
- 基于GD2圖形庫的PHP生成圖片縮略圖類代碼分享
- 支持png透明圖片的php生成縮略圖類分享
- php面向?qū)ο笾衧tatic靜態(tài)屬性與方法的內(nèi)存位置分析
- php中g(shù)et_object_vars()方法用法實(shí)例
- php面向?qū)ο笾衧tatic靜態(tài)屬性和靜態(tài)方法的調(diào)用
- php延遲靜態(tài)綁定實(shí)例分析
- php定時執(zhí)行任務(wù)設(shè)置詳解
- 遷移PHP版本到PHP7
- php include類文件超時問題處理
- 相關(guān)鏈接:
- 教程說明:
PHP教程-Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)
。