Windwork 緩存組件
Windwork 緩存組件
服務器端緩存是將程序需要頻繁訪問的網絡內容存放在本地文件或緩存服務器中,緩存過期前直接從緩存中讀取內容,以提高訪問速度。
如頻繁訪問但不經常更新的一些數據庫查詢、微信接口令牌等。
Windwork 緩存組件提供簡易健壯的緩存組件,5分鍾即可完全掌握。
目前支持文件緩存、Memcache、Memcached、Redis緩存存儲。
緩存讀寫
默認使用文件緩存
// 緩存參數
$cfg = [
'enabled' => 1, // 是否啟用緩存
'dir' => 'data/cache', // 緩存文件夾,如果使用緩存服務器,則是緩存變量的前綴
'expire' => 3600, // 緩存更新周期(默認:3600s)
'compress' => 0, // 是否啟用緩存內容壓縮後存貯
'class' => '\\wf\\cache\\adapter\\File', // 緩存模式,File)文件緩存;Memcache)使用Memcache緩存;Memcached)使用Memcached緩存;Redis)使用Redis緩存
];
$cache = new $cfg['class']($cfg);
// 從緩存讀取數據
if(null === ($ret = $cache->read('cache/key'))) {
// 緩存中無數據則初始化並寫入緩存,下次就可以直接從緩存中獲取
$ret = '緩存內容'; // 可以是標量或數組內容,不能是資源類型
$cache->write('cache/key', $ret);
}
// 在這裏使用緩存過的$ret變量
刪除緩存內容
// 刪除一條緩存
$cache->delete('cache/key');
// 清空全部緩存
$cache->clear();
// 通過前綴清空緩存
$cache->clear('user/info'); // 清空所有以 'user/info/' 開頭的緩存內容
使用memcache/memcached緩存
// Memcache
$cfg = [
'enabled' => 1, // 是否啟用緩存
'dir' => 'data/cache', // 緩存文件夾,如果使用緩存服務器,則是緩存變量的前綴
'expire' => 3600, // 緩存更新周期(默認:3600s)
'compress' => 0, // 是否啟用緩存內容壓縮後存貯
'class' => '\\wf\\cache\\adapter\\Memcache', // 緩存模式,File)文件緩存;Memcache)使用Memcache緩存;Memcached)使用Memcached緩存;Redis)使用Redis緩存
['memcache'] => [
'host' => '127.0.0.1', //
'port' => 11211, //
'pconnect' => 1, //
'timeout' => 1, // 超時時間(秒)
],
];
$cache = new $cfg['class']($cfg);
// Memcached
$cfg = [
'enabled' => 1, // 是否啟用緩存
'dir' => 'data/cache', // 緩存文件夾,如果使用緩存服務器,則是緩存變量的前綴
'expire' => 3600, // 緩存更新周期(默認:3600s)
'compress' => 0, // 是否啟用緩存內容壓縮後存貯
'class' => '\\wf\\cache\\adapter\\Memcached', // 緩存模式,File)文件緩存;Memcache)使用Memcache緩存;Memcached)使用Memcached緩存;Redis)使用Redis緩存
['memcached'] => [
'host' => '127.0.0.1', //
'port' => 11211, //
'pconnect' => 1, //
'timeout' => 1, // 超時時間(秒)
],
];
$cache = new $cfg['class']($cfg);
使用Redis緩存
使用phpredis擴展進行操作
https://github.com/phpredis/phpredis
Windows php_redis.dll 模塊下載(根據你的PHP版本選擇下載)
https://windows.php.net/downloads/pecl/snaps/redis/
$cfg = [
'enabled' => 1, // 是否啟用緩存
'dir' => 'data/cache', // 緩存文件夾,如果使用緩存服務器,則是緩存變量的前綴
'expire' => 3600, // 緩存更新周期(默認:3600s)
'compress' => 0, // 是否啟用緩存內容壓縮後存貯
'class' => '\\wf\\cache\\adapter\\Redis', // 緩存模式,File)文件緩存;Memcache)使用Memcache緩存;Redis)使用Redis緩存
'redis' => [
'host' => '127.0.0.1', //
'port' => 6379, //
'pconnect' => 1, //
'password' => '', // redis密碼,不需要密碼驗證則留空
'timeout' => 0, // 超時時間(秒),0為不限
],
];
$cache = new $cfg['class']($cfg);
在Windwork中使用緩存組件
在Windwork項目中設置好緩存配置後,使用cache()函數訪問緩存對象實例。
1、在配置文件 config/app.php中設置緩存配置(默認使用文件緩存)
// config/app.php
return [
// 緩存組件設置
'cache' => [
'enabled' => 1, // 是否啟用緩存
'class' => '\\wf\\cache\\adapter\\File', // 緩存模式,File|Memcache|Memcached|Redis
'dir' => dirname(__DIR__) . '/data/cache', // 緩存文件夾
'expire' => 7200, // 緩存更新周期(默認:7200s)
'compress' => 0, // 是否啟用緩存內容壓縮後存貯(建議隻在虛擬主機中使用文件緩存時啟用,以便省出存儲空間)
// redis
'redis' => [
'host' => '127.0.0.1', //
'port' => 6379, //
'pconnect' => 1, //
'timeout' => 0, // 超時時間(秒),0為不限
],
// memcache
'memcache' => [
'host' => '127.0.0.1', //
'port' => 11211, //
'pconnect' => 1, //
'timeout' => 1, // 超時時間(秒),0為不限
],
// memcached
'memcached' => [
'host' => '127.0.0.1', //
'port' => 11211, //
'pconnect' => 1, //
'timeout' => 1, // 超時時間(秒),0為不限
],
],
];
2、使用緩存組件
// 讀取
cache()->read('some/var/data');
// 寫入
cache()->write('some/scalar/data/key', 'some data');
cache()->write('some/array/data/key', ['some data 1', 'some data 2']);
// 刪除'some/scalar/data/key'緩存
cache()->delete('some/scalar/data/key');
// 清除some/*緩存
cache()->clear('some');
// 清除全部緩存
cache()->clear();
最後更新:2017-04-13 07:00:24