Make object cache configurable via the admin UI

This commit is contained in:
Kijin Sung 2016-02-11 13:32:55 +09:00
parent 225f02cac2
commit f3d3122787
10 changed files with 113 additions and 6 deletions

View file

@ -103,9 +103,19 @@ class CacheHandler extends Handler
*
* @return boolean
*/
public function isSupport()
public function isSupport($type = null, $cache_config = null)
{
return ($this->handler && $this->handler->isSupport());
if ($type === null)
{
return ($this->handler && $this->handler->isSupport());
}
else
{
$class = 'Cache' . ucfirst(str_replace('memcached', 'memcache', $type));
include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class);
$handler = $class::getInstance($cache_config, true);
return $handler->isSupport();
}
}
/**
@ -129,6 +139,7 @@ class CacheHandler extends Handler
*/
public function get($key, $modified_time = 0)
{
if (!$key) return false;
return $this->handler ? $this->handler->get($this->getCacheKey($key), $modified_time) : false;
}
@ -144,6 +155,7 @@ class CacheHandler extends Handler
*/
public function put($key, $obj, $valid_time = 0)
{
if (!$key) return false;
return $this->handler ? $this->handler->put($this->getCacheKey($key), $obj, $valid_time) : false;
}
@ -155,6 +167,7 @@ class CacheHandler extends Handler
*/
public function delete($key)
{
if (!$key) return false;
return $this->handler ? $this->handler->delete($this->getCacheKey($key)) : false;
}
@ -168,6 +181,7 @@ class CacheHandler extends Handler
*/
public function isValid($key, $modified_time = 0)
{
if (!$key) return false;
return $this->handler ? $this->handler->isValid($this->getCacheKey($key), $modified_time) : false;
}

View file

@ -21,11 +21,12 @@ class CacheMemcache extends CacheBase
* @param string $url url of memcache
* @return CacheMemcache instance of CacheMemcache
*/
function getInstance($url)
function getInstance($url, $force_new_instance = false)
{
if(!$GLOBALS['__CacheMemcache__'])
if(!$GLOBALS['__CacheMemcache__'] || $force_new_instance)
{
$GLOBALS['__CacheMemcache__'] = new CacheMemcache($url);
unset($GLOBALS['XE_MEMCACHE_SUPPORT']);
}
return $GLOBALS['__CacheMemcache__'];
}

View file

@ -21,9 +21,9 @@ class CacheRedis extends CacheBase
* @param string $url url of Redis
* @return CacheRedis instance of CacheRedis
*/
function getInstance($url)
function getInstance($url, $force_new_instance = false)
{
if(!$GLOBALS['__CacheRedis__'])
if(!$GLOBALS['__CacheRedis__'] || $force_new_instance)
{
$GLOBALS['__CacheRedis__'] = new CacheRedis($url);
}

View file

@ -656,6 +656,28 @@ class adminAdminController extends admin
'domain' => preg_replace('@^https?://@', '', $default_url),
));
// Object cache
if ($vars->object_cache_type)
{
if ($vars->object_cache_type === 'memcached' || $vars->object_cache_type === 'redis')
{
$cache_config = $vars->object_cache_type . '://' . $vars->object_cache_host . ':' . intval($vars->object_cache_port);
}
else
{
$cache_config = $vars->object_cache_type;
}
if (!CacheHandler::isSupport($vars->object_cache_type, $cache_config))
{
return new Object(-1, 'msg_cache_handler_not_supported');
}
Rhymix\Framework\Config::set('cache', array($cache_config));
}
else
{
Rhymix\Framework\Config::set('cache', array());
}
// Other settings
Rhymix\Framework\Config::set('use_rewrite', $vars->use_rewrite === 'Y');
Rhymix\Framework\Config::set('use_sso', $vars->use_sso === 'Y');

View file

@ -450,6 +450,27 @@ class adminAdminView extends admin
Context::set('http_port', Rhymix\Framework\Config::get('url.http_port'));
Context::set('https_port', Rhymix\Framework\Config::get('url.https_port'));
// Object cache
$object_cache_config = Rhymix\Framework\Config::get('cache');
if (is_array($object_cache_config))
{
$object_cache_config = array_first($object_cache_config);
}
$object_cache_types = array('apc', 'file', 'memcached', 'redis', 'wincache');
$object_cache_type = preg_match('/^(' . implode('|', $object_cache_types) . ')/', $object_cache_config, $matches) ? $matches[1] : '';
Context::set('object_cache_types', $object_cache_types);
Context::set('object_cache_type', $object_cache_type);
if ($object_cache_type)
{
Context::set('object_cache_host', parse_url($object_cache_config, PHP_URL_HOST) ?: null);
Context::set('object_cache_port', parse_url($object_cache_config, PHP_URL_PORT) ?: null);
}
else
{
Context::set('object_cache_host', null);
Context::set('object_cache_port', null);
}
// Other settings
Context::set('use_mobile_view', Rhymix\Framework\Config::get('use_mobile_view'));
Context::set('use_rewrite', Rhymix\Framework\Config::get('use_rewrite'));

View file

@ -86,6 +86,11 @@ $lang->about_minify_scripts = 'Automatically minify all CSS and JS scripts in th
$lang->use_gzip = 'gzip Compression';
$lang->delay_session = 'Delay session start';
$lang->about_delay_session = 'To improve performance when using a caching proxy server such as Varnish, do not issue sessions to visitors until they log in.<br>Selecting this option may cause view counts and visitor counts to become inaccurate.';
$lang->use_object_cache = 'Use Object Cache';
$lang->use_object_cache_do_not_use = 'none';
$lang->cache_host = 'Host';
$lang->cache_port = 'Port';
$lang->msg_cache_handler_not_supported = 'Your server does not support the selected cache method, or Rhymix is unable to use the cache with the given settings.';
$lang->msg_invalid_default_url = 'The default URL is invalid.';
$lang->msg_default_url_ssl_inconsistent = 'In order to use SSL always, the default URL must also begin with https://';
$lang->msg_default_url_http_port_inconsistent = 'In order to change the HTTP port, the default URL must also include the port number.';

View file

@ -86,6 +86,11 @@ $lang->about_minify_scripts = 'コアとすべてのモジュールに含まれ
$lang->use_gzip = 'gzip 圧縮';
$lang->delay_session = 'セッションの開始を遅延';
$lang->about_delay_session = 'Varnishなどのプロキシキャッシュサーバ使用時のパフォーマンスを向上させるために、ログインしていないユーザーには、認証セッションを付与しません。<br>このオプションを選択した場合、訪問者数とヒット集計が正確でない場合があります。';
$lang->use_object_cache = 'オブジェクトキャッシュ';
$lang->use_object_cache_do_not_use = '使用していない';
$lang->cache_host = 'ホスト';
$lang->cache_port = 'ポート';
$lang->msg_cache_handler_not_supported = '選択したキャッシュ方式をサーバーでサポートされていないか、与えられた情報でキャッシュにアクセスすることができません。';
$lang->msg_invalid_default_url = '基本URLが正しくありません。';
$lang->msg_default_url_ssl_inconsistent = 'SSLを常に使用する場合、基本URLもhttps//で始まる必要があります。';
$lang->msg_default_url_http_port_inconsistent = 'HTTPポートを変更する場合、基本URLも同じポートが含まれている必要があります。';

View file

@ -86,6 +86,11 @@ $lang->about_minify_scripts = '코어와 모든 모듈에 포함된 CSS, JS 파
$lang->use_gzip = 'gzip 압축';
$lang->delay_session = '세션 시작 지연';
$lang->about_delay_session = 'Varnish 등의 프록시 캐싱 서버 사용시 성능 개선을 위해, 로그인하지 않은 사용자에게는 인증 세션을 부여하지 않습니다.<br>이 옵션을 선택할 경우 방문자 수 및 조회수 집계가 정확하게 이루어지지 않을 수 있습니다.';
$lang->use_object_cache = '오브젝트 캐시 사용';
$lang->use_object_cache_do_not_use = '사용하지 않음';
$lang->cache_host = '호스트';
$lang->cache_port = '포트';
$lang->msg_cache_handler_not_supported = '선택하신 캐시 방식을 서버에서 지원하지 않거나, 주어진 정보로 캐시에 접속할 수 없습니다.';
$lang->msg_invalid_default_url = '기본 URL이 올바르지 않습니다.';
$lang->msg_default_url_ssl_inconsistent = 'SSL을 항상 사용하실 경우 기본 URL도 https://로 시작해야 합니다.';
$lang->msg_default_url_http_port_inconsistent = 'HTTP 포트를 변경하실 경우 기본 URL에도 동일한 포트가 포함되어야 합니다.';

View file

@ -58,6 +58,19 @@
<label for="use_db_session_n" class="x_inline"><input type="radio" name="use_db_session" id="use_db_session_n" value="N" checked="checked"|cond="!$use_db_session" /> {$lang->cmd_no}</label>
</div>
</div>
<div class="x_control-group">
<label class="x_control-label">{$lang->use_object_cache}</label>
<div class="x_controls">
<select name="object_cache_type" id="object_cache_type">
<option value="">{$lang->use_object_cache_do_not_use}</option>
<option value="{$key}" loop="$object_cache_types=>$key" selected="selected"|cond="$key==$object_cache_type">{$key}</option>
</select>
<div id="object_cache_additional_config" class="x_inline" style="display:none;margin-left:16px">
<label for="object_cache_host" class="x_inline">{$lang->cache_host}: <input type="text" name="object_cache_host" id="object_cache_host" value="{$object_cache_host}" /></label>
<label for="object_cache_port" class="x_inline">{$lang->cache_port}: <input type="number" name="object_cache_port" id="object_cache_port" size="5" style="min-width:70px" value="{$object_cache_port}" /></label>
</div>
</div>
</div>
<div class="x_control-group">
<label class="x_control-label">{$lang->minify_scripts}</label>
<div class="x_controls">

View file

@ -2,6 +2,27 @@ jQuery(function($){
$('.tgContent ul').bind('click', function(){
$('#sitefind_addBtn').css('display','');
});
if ($("#object_cache_type").size()) {
$("#object_cache_type").on("change", function() {
if ($(this).val().match(/memcache|redis/)) {
$("#object_cache_additional_config").show();
if (!$("#object_cache_host").val()) {
$("#object_cache_host").val('127.0.0.1');
}
if (!$("#object_cache_port").val()) {
$("#object_cache_port").val($(this).val().match(/memcache/) ? '11211' : '6379');
}
if ($(this).val().match(/memcache/) && $("#object_cache_port").val() == '6379') {
$("#object_cache_port").val('11211');
}
if ($(this).val().match(/redis/) && $("#object_cache_port").val() == '11211') {
$("#object_cache_port").val('6379');
}
} else {
$("#object_cache_additional_config").hide();
}
}).triggerHandler("change");
}
});
function setStartModule(){