PHP code example of easyswoole / cache

1. Go to this page and download the library: Download easyswoole/cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

easyswoole / cache example snippets



use EasySwoole\Cache\Cache;

$ttl = 10;  // 单位秒
$cacheKey = 'cacheKey';
$cacheValue = 'cacheValue';
$defaultValue = 'defaultValue';

Cache::clear();
Cache::has($cacheKey);
Cache::delete($cacheKey);
Cache::set($cacheKey, $cacheValue, $ttl);
Cache::get($cacheKey, $defaultValue);
Cache::setMultiple(['CacheKey1' => 'CacheValue1',], $ttl);
Cache::getMultiple(['CacheKey1'], $defaultValue);
Cache::deleteMultiple(['CacheKey1']);



// 请注意先Use要使用的驱动类和配置类
use EasySwoole\Cache\Config\FileConfig;
use EasySwoole\Cache\Drivers\File as FileDriver;

// 也可以不传入Config 自动使用下面的默认值
$fileDriver = new FileDriver((new FileConfig([
    'cachePath'     => sys_get_temp_dir(),   // 缓存目录(默认为PHP系统缓存)
    'cachePrefix'   => null, // 默认的缓存前缀 不同前缀分目录存放
    'defaultExpire' => 0,    // 默认过期时间 设置为0永不过期
])));

// 注册驱动时不指定驱动名称,则为注册default驱动
// 仅default驱动可以多次注册,实际调用最后一次注册的default驱动
// 其他驱动名称仅允许注册一次,不允许重复注册相同名称的驱动
Cache::instance()->addDriver($fileDriver,'default');

// 从缓存管理器单例中获取任意名字的驱动,不指定名字为获取default驱动
$cache = Cache::instance()->getDriver('default');



// 请注意先Use要使用的驱动类和配置类
use EasySwoole\Cache\Config\RedisConfig;
use EasySwoole\Cache\Drivers\Redis as RedisDriver;

// 需要协程环境(在EasySwoole框架内无需手动创建协程)
Coroutine::create(function () {

    // Redis的Config可以同时配置链接池相关的设置项
    $redisDriver = new RedisDriver((new RedisConfig([

        // 链接配置项
        'db'                => 0,           // 有多个DB时可以选择使用的DB 默认选择0号DB
        'host'              => '127.0.0.1', // 默认是连接本地Redis
        'port'              => 6379,        // 默认的端口
        'auth'              => null,        // 默认没有密码
        'connectTimeout'    => 1,           // 连接到服务器的超时时间
        'execTimeout'       => 1,           // 执行操作的超时时间
        'reconnect'         => 3,           // 如果连接断开自动尝试x次重连

        // 链接池配置项
        'intervalCheckTime' => 30 * 1000,   // 池对象回收检测周期
        'maxIdleTime'       => 15,          // 连接最大空闲时间(超时释放)
        'maxObjectNum'      => 20,          // 池最大连接象数量
        'minObjectNum'      => 5,           // 保持的最小连接数量
        'getObjectTimeout'  => 3.0          // 池为空时获取连接最大等待时间

    ])));

    Cache::instance()->addDriver($redisDriver, 'redis');
    $cache = Cache::instance()->getDriver('redis');
});



// 请注意先Use要使用的驱动类和配置类
use EasySwoole\Cache\Config\MemcacheConfig;
use EasySwoole\Cache\Drivers\Memcache as MemcacheDriver;

// 需要协程环境(在EasySwoole框架内无需手动创建协程)
Coroutine::create(function () {

    // Memcache的Config可以同时配置链接池相关的设置项
    $memcacheDriver = new MemcacheDriver((new MemcacheConfig([

        // 链接配置项
        'host'              => '127.0.0.1',  // 默认是连接本地Memcache
        'port'              => 11211,        // 默认的端口

        // 链接池配置项
        'intervalCheckTime' => 30 * 1000,   // 池对象回收检测周期
        'maxIdleTime'       => 15,          // 连接最大空闲时间(超时释放)
        'maxObjectNum'      => 20,          // 池最大连接象数量
        'minObjectNum'      => 5,           // 保持的最小连接数量
        'getObjectTimeout'  => 3.0          // 池为空时获取连接最大等待时间

    ])));

    Cache::instance()->addDriver($memcacheDriver, 'memcache');
    $cache = Cache::instance()->getDriver('memcache');
});



// 请注意先Use要使用的驱动类和配置类
use EasySwoole\Cache\Config\SwooleTableConfig;
use EasySwoole\Cache\Drivers\SwooleTable as SwooleTableDriver;

// 需要协程环境(在EasySwoole框架内无需手动创建协程)
Coroutine::create(function () {

    // 可以配置Table相关的一些配置项
    $swooleTableDriver = new SwooleTableDriver((new SwooleTableConfig([

        'tableSize'          => 4096,  // 能容纳的总Key数量(由于哈希冲突,实际储存量会比该值小一点)
        'maxKeySize'         => 512,   // Key支持的最大长度(字节)
        'maxValueSize'       => 4096,  // Value支持的最大长度(字节)
        'recycleInterval'    => 1000,  // 超时Key回收周期(ms)
        'conflictProportion' => 0.2,   // 允许哈希冲突的最大比例

    ])));

    Cache::instance()->addDriver($swooleTableDriver, 'swTable');
    $cache = Cache::instance()->getDriver('swTable');
});



use EasySwoole\Cache\Config\SwooleTableConfig;

$tableConfig = new SwooleTableConfig;

$tableConfig
    ->setTableSize(4096)
    ->setMaxKeySize(512)
    ->setMaxValueSize(512);