PHP code example of kylin987 / orm-redis-cache
1. Go to this page and download the library: Download kylin987/orm-redis-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/ */
kylin987 / orm-redis-cache example snippets
return [
'cache_store' => 'default', // config/redis.php 中的连接名
'cache_exptime' => 300, // 必须 > 0,模型 cacheExpTime 可覆盖
'shared_prefix' => 'ormrc:v1',
'error_handler' => null, // callable(string $operation, Throwable $error)
];
class Product extends \support\Model
{
use \Kylin987\Orm\RedisCache\Eloquent\EloquentCache;
protected $table = 'kfc_product';
protected $redisCacheScope = 'catering-main'; // 显式开启
protected $cachePk = ['tenant_id', 'id']; // 固定唯一组合
protected $cacheExpTime = 300;
}
$product = Product::getRedisCache(['tenant_id' => 10, 'id' => 20]);
$fresh = Product::getRedisCache(['id' => 20, 'tenant_id' => 10], true);
Product::delCache(new Product());
class Product extends \think\Model
{
use \Kylin987\Orm\RedisCache\Webman\ThinkOrmCache;
// ThinkPHP 改用:\Kylin987\Orm\RedisCache\ThinkPhp\ThinkOrmCache
protected $name = 'kfc_product';
protected $redisCacheScope = 'catering-main';
protected $cachePk = ['tenant_id', 'id'];
protected $cacheExpTime = 300;
public static function onAfterInsert($model): void { self::delCache($model); }
public static function onAfterUpdate($model): void { self::delCache($model); }
public static function onAfterDelete($model): void { self::delCache($model); }
public static function onAfterRestore($model): void { self::delCache($model); }
}
$product = Product::getRedisCache(['tenant_id' => 10, 'id' => 20]);
// Webman: config/think-orm.php;ThinkPHP: config/database.php 的对应连接
'type' => \Kylin987\Orm\RedisCache\ThinkOrm\Mysql::class,
\support\Db::transaction(function () {
Product::where('tenant_id', 10)->update(['enabled' => 0]);
Product::delCache(new Product());
});
sh
php tests/install.php
php tests/dependencies.php
php tests/dependencies.php unsupported
CACHE_TEST_REDIS_SOCKET=/tmp/your-isolated-redis.sock php tests/run.php