windwalker / cache
Windwalker Cache package
Installs: 3 635
Dependents: 5
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 1
Type:windwalker-package
Requires
- php: >=7.1.3
- ext-json: *
Requires (Dev)
- windwalker/test: ~3.0
Provides
- psr/cache-implementation: ~1.0
- 4.x-dev
- 3.x-dev
- dev-master / 3.x-dev
- 3.5.22
- 3.5.21
- 3.5.20
- 3.5.19
- 3.5.18
- 3.5.17
- 3.5.16
- 3.5.15
- 3.5.14
- 3.5.13
- 3.5.12
- 3.5.11
- 3.5.10
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0.1
- 3.0
- 3.0-beta2
- 3.0-beta
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta2
- 2.0.0-beta1
- 2.0.0-alpha
- dev-test
This package is auto-updated.
Last update: 2021-01-07 17:10:10 UTC
README
Windwalker Cache package provides an simple interface to easily store and fetch cache files.
Installation via Composer
Add this to the require block in your composer.json
.
{ "require": { "windwalker/cache": "~3.0" } }
Getting Started
Create a cache object and store data.
use Windwalker\Cache\Cache; $data = array('sakura'); $cache = new Cache; $cache->set('flower', $data);
Then we can get this data by same key.
$data = $cache->get('flower'); // return Array('sakura')
Auto Fetch Data By Callback
Using call method to auto detect is cache exists or not.
$data = $cache->call('flower', function() { return array('sakura'); });
It is same as this code:
if (!$cache->exists('flower')) { $cache->set('flower', array('sakura')); } $data = $cache->get('flower');
Storage
Set storage to Cache so we can use different storage engine to save cache data.
use Windwalker\Cache\Cache; use Windwalker\Cache\Storage\ArrayStorage; $cache = new Cache(new ArrayStorage);
ArrayStorage and RuntimeArrayStorage
This is default storage, which will store data in itself and will not depends on any outside storage engine.
The RuntimeArrayStorage
use static property to storage data, which means all data will live in current runtime
no matter how many times you create it.
FileStorage
Create a cache with FileStorage
and set a path to store files.
use Windwalker\Cache\Cache; use Windwalker\Cache\Storage\FileStorage; $path = '/your/cache/path'; $cache = new Cache(new FileStorage($path)); $cache->set('flower', array('sakura'));
The file will store at /your/cache/path/~5a46b8253d07320a14cace9b4dcbf80f93dcef04.data
, and the data will be serialized string.
a:1:{i:0;s:6:"sakura";}
File Group
Group is a subfolder of your storage path.
$path = '/your/cache/path'; $cache = new Cache(new FileStorage($path, 'mygroup')); $cache->set('flower', array('sakura'));
The file wil store at /your/cache/path/mygroup/~5a46b8253d07320a14cace9b4dcbf80f93dcef04.data
that for organize your cache folder.
PHP File Format and Deny Access
If your cache folder are exposure on web environment, we have to make our cache files unable to access. The argument 3
of FileStorage
is use to deny access.
$path = '/your/cache/path'; $cache = new Cache(new FileStorage($path, 'mygroup', true)); $cache->set('flower', array('sakura'));
The stored file will be a PHP file with code to deny access:
/your/cache/path/mygroup/~5a46b8253d07320a14cace9b4dcbf80f93dcef04.php
<?php die("Access Deny"); ?>a:1:{i:0;s:6:"sakura";}
Available Storage
- ArrayStorage
- RuntimeArrayStorage
- FileStorage
- PhpFileStorage (Use include instead read text)
- ForeverFileStorage (Never expired file storage)
- MemcachedStorage
- RedisStorage
- WincacheStorage
- XcacheStorage
- NullStorage
Serializer
The default PhpSerializer
will make our data be php serialized string, if you want to use other format,
just change serializer at second argument of Cache object.
use Windwalker\Cache\Cache; use Windwalker\Cache\Serializer\JsonSerializer; use Windwalker\Cache\Storage\FileStorage; $cache = new Cache(new FileStorage(__DIR__ . '/cache'), new JsonSerializer); $cache->set('flower', array('flower' => 'sakura'));
The stored cache file is:
{"flower":"sakura"}
Full Page Cache
Sometimes we may need to store whole html as static page cache. StringSerializer
or RawSerializer
helps us save raw data as string:
use Windwalker\Cache\Cache; use Windwalker\Cache\Serializer\StringSerializer; use Windwalker\Cache\Storage\FileStorage; $cache = new Cache(new FileStorage($path), new StringSerializer); $url = 'http://mysite.com/foo/bar/baz'; if ($cache->exists($url)) { echo $cache->get($url); exit(); } $html = View::render('html.layout'); $cache->set($url, $html); echo $html;
PhpFileSerializer
This serializer can save array data as a php file, will be useful when we need to cache config data.
use Windwalker\Cache\Cache; use Windwalker\Cache\Serializer\PhpSerializer; use Windwalker\Cache\Storage\FileStorage; $cache = new Cache(new FileStorage($path), new PhpSerializer); $config = array('foo' => 'bar'); $cache->set('config.name', $config); $cache->get('config.name'); // Array( [foo] => bar )
The cache file will be:
<?php return array ( 'foo' => 'bar', );
Supported Serializer
- PhpSerializer
- PhpFileSerializer
- JsonSerializer
- StringSerializer
- RawSerializer
PSR6 Cache Interface
Windwalker Cache Storage are all follows PSR6, so you can use other libraries' CacheItemPool object as storage, you can also directly use Storage object.
use Windwalker\Cache\Item\CacheItem; use Windwalker\Cache\Storage\FileStorage; $cachePool = new FileStorage(__DIR__ . '/cache'); $cachePool->save(new CacheItem('foo', 'Bar', 150)); // OR save differed $cachePool->saveDeferred(new CacheItem('baz', 'Yoo', 150)); $cachePool->commit();