123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace app\common\model;
- use think\Model;
- use app\common\Redis;
- use think\facade\Config;
- /**
- * Description of RedisBaseModel
- *
- * @author sgq
- */
- class RedisBaseModel extends Model {
- protected static function onAfterWrite(Model $model): void {
- $data = $model->toArray();
- if ($data["id"]) {
- $redis = Redis::instance(Config::get("cache.stores.redis.select"));
- $redis->hSet($model->name, $data["id"], json_encode($data));
- }
- }
- protected static function onAfterDelete(Model $model): void {
- $data = $model->toArray();
- if ($data["id"]) {
- $redis = Redis::instance(Config::get("cache.stores.redis.select"));
- $redis->hDel($model->name, $data["id"]);
- }
- }
- public static function cache($id = 0) {
- $redis = Redis::instance(Config::get("cache.stores.redis.select"));
- if ($id) {
- $data = self::select($id);
- $name = $data->name;
- $redis->hSet($name, $id, json_encode($data));
- } else {
- $list = self::select();
- $name = $list[0]->name;
- $redis->del($name);
- foreach ($list as $data) {
- $redis->hSet($name, $data["id"], json_encode($data));
- }
- }
- }
- }
|