RedisBaseModel.php 775 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\Redis;
  5. use think\facade\Config;
  6. /**
  7. * Description of RedisBaseModel
  8. *
  9. * @author sgq
  10. */
  11. class RedisBaseModel extends Model {
  12. protected static function onAfterWrite(Model $model): void {
  13. $data = $model->toArray();
  14. if ($data["id"]) {
  15. $redis = Redis::instance(Config::get("cache.stores.redis.select"));
  16. $redis->hSet($model->name, $data["id"], json_encode($data));
  17. }
  18. }
  19. protected static function onAfterDelete(Model $model): void {
  20. $data = $model->toArray();
  21. if ($data["id"]) {
  22. $redis = Redis::instance(Config::get("cache.stores.redis.select"));
  23. $redis->hDel($model->name, $data["id"]);
  24. }
  25. }
  26. }