刮刮后端接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

<?php
namespace app\model;
use think\facade\Cache;
use think\Model;
class Setting extends Model
{
/**
* 添加设置
* @param $key
* @param $value
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function settingSave($key,$value)
{
$Setting = new Setting();
$edit = $Setting->where('key',$key)->find();
$value = serialize($value);
if ($edit) {
$edit->value = $value;
$Setting = $edit->save();
} else {
$Setting = $Setting->save(['key' => $key, 'value' => $value]);
}
# 添加缓存
Cache::store('redis')->set($key,$value);
return true;
}
/**
* 获取缓存
* @param $key
* @return array|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function settingLoad($key)
{
# 获取缓存
$catch = Cache::store('redis')->get($key);
if (empty($catch)) {
$Setting = new Setting();
$Setting = $Setting->where('key',$key)->find();
if (!$Setting) return '';
$catch = $Setting->value;
# 添加缓存
Cache::store('redis')->set($key,$catch);
}
return unserialize($catch);
}
}