Skip to content

Commit

Permalink
cache方法更名为S方法 原S方法废弃
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 13, 2012
1 parent f7b6848 commit 4663e3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 46 deletions.
50 changes: 7 additions & 43 deletions ThinkPHP/Common/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,68 +369,32 @@ function redirect($url, $time=0, $msg='') {
* 缓存管理
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置
* @param mixed $value 缓存值
* @param mixed $expire 缓存有效期(秒)
* @param mixed $options 缓存参数
* @return mixed
*/
function cache($name,$value='',$expire=0) {
function S($name,$value='',$options=null) {
static $cache = '';
if(is_array($expire)){
if(is_array($options)){
// 缓存操作的同时初始化
$type = isset($expire['type'])?$expire['type']:'';
$cache = Cache::getInstance($type,$expire);
$type = isset($options['type'])?$options['type']:'';
$cache = Cache::getInstance($type,$options);
}elseif(is_array($name)) { // 缓存初始化
$type = isset($name['type'])?$name['type']:'';
$cache = Cache::getInstance($type,$name);
return $cache;
}elseif(empty($cache)) { // 自动初始化
$cache = Cache::getInstance();
}
if(''=== $value){ // 获取缓存值
// 获取缓存数据
if(''=== $value){ // 获取缓存
return $cache->get($name);
}elseif(is_null($value)) { // 删除缓存
return $cache->rm($name);
}else { // 缓存数据
$expire = is_numeric($expire)?$expire:NULL;
$expire = is_numeric($options)?$options:NULL;
return $cache->set($name, $value, $expire);
}
}

/**
* 全局缓存设置和读取
* @param string $name 缓存名称
* @param mixed $value 缓存值
* @param integer $expire 缓存有效期(秒)
* @param string $type 缓存类型
* @param array $options 缓存参数
* @return mixed
*/
function S($name, $value='', $expire=null, $type='',$options=null) {
static $_cache = array();
//取得缓存对象实例
$cache = Cache::getInstance($type,$options);
if ('' !== $value) {
if (is_null($value)) {
// 删除缓存
$result = $cache->rm($name);
if ($result)
unset($_cache[$type . '_' . $name]);
return $result;
}else {
// 缓存数据
$cache->set($name, $value, $expire);
$_cache[$type . '_' . $name] = $value;
}
return;
}
if (isset($_cache[$type . '_' . $name]))
return $_cache[$type . '_' . $name];
// 获取缓存数据
$value = $cache->get($name);
$_cache[$type . '_' . $name] = $value;
return $value;
}

/**
* 快速文件数据读取和保存 针对简单类型数据 字符串、数组
* @param string $name 缓存名称
Expand Down
6 changes: 3 additions & 3 deletions ThinkPHP/Lib/Core/Db.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,14 @@ public function select($options=array()) {
$cache = isset($options['cache'])?$options['cache']:false;
if($cache) { // 查询缓存检测
$key = is_string($cache['key'])?$cache['key']:md5($sql);
$value = S($key,'','',$cache['type']);
$value = S($key,'',$cache);
if(false !== $value) {
return $value;
}
}
$result = $this->query($sql);
if($cache && false !== $result ) { // 查询缓存写入
S($key,$result,$cache['expire'],$cache['type']);
S($key,$result,$cache);
}
return $result;
}
Expand Down Expand Up @@ -769,7 +769,7 @@ public function buildSelectSql($options=array()) {
$sql = $this->parseSql($this->selectSql,$options);
$sql .= $this->parseLock(isset($options['lock'])?$options['lock']:false);
if(isset($key)) { // 写入SQL创建缓存
S($key,$sql,0,'',array('length'=>C('DB_SQL_BUILD_LENGTH'),'queue'=>C('DB_SQL_BUILD_QUEUE')));
S($key,$sql,array('expire'=>0,'length'=>C('DB_SQL_BUILD_LENGTH'),'queue'=>C('DB_SQL_BUILD_QUEUE')));
}
return $sql;
}
Expand Down

0 comments on commit 4663e3c

Please sign in to comment.