crack9527 / swoft-cache
There is no license information available for the latest version (1.0.5) of this package.
1.0.5
2019-10-23 02:32 UTC
Requires
- swoft/framework: ~2.0.0
- swoft/redis: ~2.0.0
- swoft/swoole-ide-helper: ^4.4
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is not auto-updated.
Last update: 2025-03-06 07:26:51 UTC
README
Swoft缓存组件
composer安装
- composer command
composer require crack9527/swoft-cache
支持组件
- redis
- file
- memcached
使用方法
- 在配置文件
app/bean.php
里增加组件配置,如使用redis缓存
'cache' => [
'class' => \Crack9527\Cache\Component\RedisCache::class,
]
- 在需要使用缓存的地方,使用全局方法
cache()
,如下
cache()->get($key);
或者通过注解注入对象属性里
<?php declare(strict_types=1); namespace App\Http\Controller; use Crack9527\Cache\Contract\CacheInterface; use Swoft; use Swoft\Bean\Annotation\Mapping\Inject; use Swoft\Http\Message\ContentType; use Swoft\Http\Message\Response; use Swoft\Http\Server\Annotation\Mapping\Controller; use Swoft\Http\Server\Annotation\Mapping\RequestMapping; use Swoft\View\Renderer; use Throwable; use Swoft\Redis\Redis; /** * Class HomeController * @Controller() */ class HomeController { /** * @Inject("cache") * @var CacheInterface */ private $cache; public function test() { $rand = uniqid(); $key = "test"; $this->cache->set($key, $rand, 3600 * 30); assert($rand === $this->cache->get($key)); } }
缓存方法如下
##设置缓存
cache()->set('name', $value, 3600);
如果设置成功返回true,否则返回false。
缓存自增
针对数值类型的缓存数据,可以使用自增操作,例如:
// name自增(步进值为1)
cache()->inc('name');
// name自增(步进值为3)
cache()->inc('name',3);
缓存自减
针对数值类型的缓存数据,可以使用自减操作,例如:
// name自减(步进值为1)
cache()->dec('name');
// name自减(步进值为3)
cache()->dec('name',3);
获取缓存
获取缓存数据可以使用:
cache()->get('name');
如果name值不存在,则默认返回 false。
支持指定默认值,例如:
cache()->get('name','');
表示如果name值不存在,则返回空字符串。
删除缓存
cache()->rm('name');
清空缓存
cache()->clear();
LICENSE
The Component is open-sourced software licensed under the Apache license.