数据缓存与session配置分离
parent
b6b7f7170d
commit
c890603737
|
|
@ -205,11 +205,25 @@ class Site extends Common
|
|||
|
||||
// 缓存
|
||||
case 'cache' :
|
||||
if((isset($params['common_session_is_use_cache']) && $params['common_session_is_use_cache'] == 1) || (isset($params['common_data_is_use_cache']) && $params['common_data_is_use_cache'] == 1))
|
||||
// session是否使用缓存
|
||||
if(isset($params['common_session_is_use_cache']) && $params['common_session_is_use_cache'] == 1)
|
||||
{
|
||||
if(!extension_loaded('redis'))
|
||||
// 连接测试
|
||||
$ret = $this->RedisCheckConnectPing($params['common_cache_session_redis_host'], $params['common_cache_session_redis_port'], $params['common_cache_session_redis_password']);
|
||||
if($ret['code'] != 0)
|
||||
{
|
||||
return DataReturn('请先安装redis扩展', -1);
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
// 数据是否使用缓存
|
||||
if(isset($params['common_data_is_use_cache']) && $params['common_data_is_use_cache'] == 1)
|
||||
{
|
||||
// 连接测试
|
||||
$ret = $this->RedisCheckConnectPing($params['common_cache_data_redis_host'], $params['common_cache_data_redis_port'], $params['common_cache_data_redis_password']);
|
||||
if($ret['code'] != 0)
|
||||
{
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -257,5 +271,50 @@ class Site extends Common
|
|||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* redis连接测试
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 1.0.0
|
||||
* @date 2020-09-26
|
||||
* @desc description
|
||||
* @param [string] $host [连接地址]
|
||||
* @param [int] $port [端口]
|
||||
* @param [string] $password [密码]
|
||||
*/
|
||||
private function RedisCheckConnectPing($host, $port, $password)
|
||||
{
|
||||
// 参数处理
|
||||
$host = empty($host) ? '127.0.0.1' : $host;
|
||||
$port = empty($port) ? 6379 : $port;
|
||||
$password = empty($password) ? '' : $password;
|
||||
|
||||
// 是否已安装redis扩展
|
||||
if(!extension_loaded('redis'))
|
||||
{
|
||||
return DataReturn('请先安装redis扩展', -1);
|
||||
}
|
||||
|
||||
// 捕获异常
|
||||
try {
|
||||
// 连接redis
|
||||
$redis = new \Redis();
|
||||
$redis->connect($host, $port);
|
||||
if($password != '')
|
||||
{
|
||||
$redis->auth($password);
|
||||
}
|
||||
} catch(\Exception $e) {
|
||||
return DataReturn('redis连接失败['.$e->getMessage().']', -1);
|
||||
}
|
||||
|
||||
// 检测是否连接成功
|
||||
if($redis->ping())
|
||||
{
|
||||
return DataReturn('redis连接成功', 0);
|
||||
}
|
||||
return DataReturn('redis连接失败', -1);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -9,10 +9,17 @@
|
|||
|
||||
<!-- form start -->
|
||||
<form class="am-form form-validation view-save" action="{{:MyUrl('admin/site/save')}}" method="POST" request-type="ajax-url" request-value="{{:MyUrl('admin/site/index', ['nav_type'=>'cache'])}}" enctype="multipart/form-data">
|
||||
<!-- 基础配置 -->
|
||||
<div class="am-alert am-alert-warning" data-am-alert>
|
||||
<button type="button" class="am-close">×</button>
|
||||
<p>1. 默认使用的文件缓存、使用Redis缓存PHP需要先安装Redis扩展</p>
|
||||
<p>2. 请确保Redis服务稳定性(Session使用缓存后、服务不稳定可能导致后台也无法登录)</p>
|
||||
<p>3. 如遇到Redis服务异常无法登录管理后台、可进入数据库[ <strong class="am-text-danger">{{:config('database.prefix')}}config</strong> ]表修改配置、然后删除[ <strong class="am-text-danger">config</strong> ]目录下[ <strong class="am-text-danger">session.php,cache.php</strong> ]文件</p>
|
||||
</div>
|
||||
|
||||
<!-- Session缓存配置 -->
|
||||
<div class="am-panel am-panel-default">
|
||||
<div class="am-panel-hd">
|
||||
<h3 class="am-panel-title">基础配置</h3>
|
||||
<h3 class="am-panel-title">Session缓存配置</h3>
|
||||
</div>
|
||||
<div class="am-panel-bd">
|
||||
<div class="am-form-group">
|
||||
|
|
@ -23,6 +30,41 @@
|
|||
{{/foreach}}
|
||||
</select>
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_session_redis_host.name}}<span class="am-form-group-label-tips">{{$data.common_cache_session_redis_host.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_session_redis_host.only_tag}}" placeholder="{{$data.common_cache_session_redis_host.describe}}" data-validation-message="{{$data.common_cache_session_redis_host.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_session_redis_host.value}}"{{/if}} />
|
||||
</div>
|
||||
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_session_redis_port.name}}<span class="am-form-group-label-tips">{{$data.common_cache_session_redis_port.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_session_redis_port.only_tag}}" placeholder="{{$data.common_cache_session_redis_port.describe}}" data-validation-message="{{$data.common_cache_session_redis_port.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_session_redis_port.value}}"{{/if}} />
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_session_redis_password.name}}<span class="am-form-group-label-tips">{{$data.common_cache_session_redis_password.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_session_redis_password.only_tag}}" placeholder="{{$data.common_cache_session_redis_password.describe}}" data-validation-message="{{$data.common_cache_session_redis_password.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_session_redis_password.value}}"{{/if}} />
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_session_redis_expire.name}}<span class="am-form-group-label-tips">{{$data.common_cache_session_redis_expire.describe}}</span></label>
|
||||
<div class="am-input-group am-input-group-sm">
|
||||
<input type="number" min="0" name="{{$data.common_cache_session_redis_expire.only_tag}}" placeholder="{{$data.common_cache_session_redis_expire.name}}" data-validation-message="{{$data.common_cache_session_redis_expire.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_session_redis_expire.value}}"{{/if}} />
|
||||
<span class="am-input-group-btn">
|
||||
<button class="am-btn am-btn-default" type="button">秒</button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_session_redis_prefix.name}}<span class="am-form-group-label-tips">{{$data.common_cache_session_redis_prefix.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_session_redis_prefix.only_tag}}" placeholder="{{$data.common_cache_session_redis_prefix.describe}}" data-validation-message="{{$data.common_cache_session_redis_prefix.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_session_redis_prefix.value}}"{{/if}} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 数据缓存配置 -->
|
||||
<div class="am-panel am-panel-default">
|
||||
<div class="am-panel-hd">
|
||||
<h3 class="am-panel-title">数据缓存配置</h3>
|
||||
</div>
|
||||
<div class="am-panel-bd">
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_data_is_use_cache.name}}<span class="am-form-group-label-tips">{{$data.common_data_is_use_cache.describe}}</span></label>
|
||||
<select name="{{$data.common_data_is_use_cache.only_tag}}" class="am-radius chosen-select" data-validation-message="{{$data.common_data_is_use_cache.error_tips}}" required>
|
||||
|
|
@ -31,45 +73,31 @@
|
|||
{{/foreach}}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Redis配置 -->
|
||||
<div class="am-panel am-panel-default">
|
||||
<div class="am-panel-hd">
|
||||
<h3 class="am-panel-title">Redis配置</h3>
|
||||
</div>
|
||||
<div class="am-panel-bd">
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_redis_host.name}}<span class="am-form-group-label-tips">{{$data.common_cache_redis_host.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_redis_host.only_tag}}" placeholder="{{$data.common_cache_redis_host.describe}}" data-validation-message="{{$data.common_cache_redis_host.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_redis_host.value}}"{{/if}} />
|
||||
<label>{{$data.common_cache_data_redis_host.name}}<span class="am-form-group-label-tips">{{$data.common_cache_data_redis_host.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_data_redis_host.only_tag}}" placeholder="{{$data.common_cache_data_redis_host.describe}}" data-validation-message="{{$data.common_cache_data_redis_host.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_data_redis_host.value}}"{{/if}} />
|
||||
</div>
|
||||
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_redis_port.name}}<span class="am-form-group-label-tips">{{$data.common_cache_redis_port.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_redis_port.only_tag}}" placeholder="{{$data.common_cache_redis_port.describe}}" data-validation-message="{{$data.common_cache_redis_port.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_redis_port.value}}"{{/if}} />
|
||||
<label>{{$data.common_cache_data_redis_port.name}}<span class="am-form-group-label-tips">{{$data.common_cache_data_redis_port.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_data_redis_port.only_tag}}" placeholder="{{$data.common_cache_data_redis_port.describe}}" data-validation-message="{{$data.common_cache_data_redis_port.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_data_redis_port.value}}"{{/if}} />
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_redis_password.name}}<span class="am-form-group-label-tips">{{$data.common_cache_redis_password.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_redis_password.only_tag}}" placeholder="{{$data.common_cache_redis_password.describe}}" data-validation-message="{{$data.common_cache_redis_password.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_redis_password.value}}"{{/if}} />
|
||||
<label>{{$data.common_cache_data_redis_password.name}}<span class="am-form-group-label-tips">{{$data.common_cache_data_redis_password.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_data_redis_password.only_tag}}" placeholder="{{$data.common_cache_data_redis_password.describe}}" data-validation-message="{{$data.common_cache_data_redis_password.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_data_redis_password.value}}"{{/if}} />
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_redis_expire.name}}<span class="am-form-group-label-tips">{{$data.common_cache_redis_expire.describe}}</span></label>
|
||||
<label>{{$data.common_cache_data_redis_expire.name}}<span class="am-form-group-label-tips">{{$data.common_cache_data_redis_expire.describe}}</span></label>
|
||||
<div class="am-input-group am-input-group-sm">
|
||||
<input type="number" min="0" name="{{$data.common_cache_redis_expire.only_tag}}" placeholder="{{$data.common_cache_redis_expire.name}}" data-validation-message="{{$data.common_cache_redis_expire.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_redis_expire.value}}"{{/if}} />
|
||||
<input type="number" min="0" name="{{$data.common_cache_data_redis_expire.only_tag}}" placeholder="{{$data.common_cache_data_redis_expire.name}}" data-validation-message="{{$data.common_cache_data_redis_expire.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_data_redis_expire.value}}"{{/if}} />
|
||||
<span class="am-input-group-btn">
|
||||
<button class="am-btn am-btn-default" type="button">秒</button>
|
||||
</span>
|
||||
</div>
|
||||
<div class="am-alert am-alert-warning" data-am-alert>
|
||||
<button type="button" class="am-close">×</button>
|
||||
<p>1. session下过期时间默认为3600秒</p>
|
||||
<p>2. 数据缓存下过期时间默认0(表示默认永久缓存)</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>{{$data.common_cache_redis_prefix.name}}<span class="am-form-group-label-tips">{{$data.common_cache_redis_prefix.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_redis_prefix.only_tag}}" placeholder="{{$data.common_cache_redis_prefix.describe}}" data-validation-message="{{$data.common_cache_redis_prefix.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_redis_prefix.value}}"{{/if}} />
|
||||
<label>{{$data.common_cache_data_redis_prefix.name}}<span class="am-form-group-label-tips">{{$data.common_cache_data_redis_prefix.describe}}</span></label>
|
||||
<input type="text" name="{{$data.common_cache_data_redis_prefix.only_tag}}" placeholder="{{$data.common_cache_data_redis_prefix.describe}}" data-validation-message="{{$data.common_cache_data_redis_prefix.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_cache_data_redis_prefix.value}}"{{/if}} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -63,15 +63,15 @@ class BaseConfigHandleService
|
|||
// 使用redis
|
||||
'type' => 'redis',
|
||||
// 连接地址
|
||||
'host' => MyC('common_cache_redis_host', '127.0.0.1', true),
|
||||
'host' => MyC('common_cache_session_redis_host', '127.0.0.1', true),
|
||||
// 端口号
|
||||
'port' => MyC('common_cache_redis_port', 6379, true),
|
||||
'port' => MyC('common_cache_session_redis_port', 6379, true),
|
||||
// 密码
|
||||
'password' => MyC('common_cache_redis_password', '', true),
|
||||
'password' => MyC('common_cache_session_redis_password', '', true),
|
||||
// 全局缓存有效期、默认3600秒
|
||||
'expire' => MyC('common_cache_redis_expire', 3600, true),
|
||||
'expire' => MyC('common_cache_session_redis_expire', 3600, true),
|
||||
// 缓存前缀
|
||||
'prefix' => MyC('common_cache_redis_prefix', 'shopxo', true),
|
||||
'prefix' => MyC('common_cache_session_redis_prefix', 'shopxo', true),
|
||||
];
|
||||
} else {
|
||||
$config = [
|
||||
|
|
@ -116,15 +116,15 @@ class BaseConfigHandleService
|
|||
// 使用redis
|
||||
'type' => 'redis',
|
||||
// 连接地址
|
||||
'host' => MyC('common_cache_redis_host', '127.0.0.1', true),
|
||||
'host' => MyC('common_cache_data_redis_host', '127.0.0.1', true),
|
||||
// 端口号
|
||||
'port' => MyC('common_cache_redis_port', 6379, true),
|
||||
'port' => MyC('common_cache_data_redis_port', 6379, true),
|
||||
// 密码
|
||||
'password' => MyC('common_cache_redis_password', '', true),
|
||||
'password' => MyC('common_cache_data_redis_password', '', true),
|
||||
// 全局缓存有效期(0为永久有效)
|
||||
'expire' => MyC('common_cache_redis_expire', 0, true),
|
||||
'expire' => MyC('common_cache_data_redis_expire', 0, true),
|
||||
// 缓存前缀
|
||||
'prefix' => MyC('common_cache_redis_prefix', 'shopxo', true),
|
||||
'prefix' => MyC('common_cache_data_redis_prefix', 'shopxo', true),
|
||||
];
|
||||
|
||||
} else {
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue