cache
This commit is contained in:
parent
94f8d20a1e
commit
dde58e385a
@ -17,6 +17,7 @@ use think\facade\Db;
|
||||
use think\facade\Session;
|
||||
use think\exception\ValidateException;
|
||||
use app\common\model\User as UserModel;
|
||||
use taoler\com\Files;
|
||||
|
||||
class Admin extends AdminController
|
||||
{
|
||||
@ -188,4 +189,18 @@ class Admin extends AdminController
|
||||
View::assign('admin',$admin);
|
||||
return View::fetch('set/user/repass');
|
||||
}
|
||||
|
||||
//清除缓存Cache
|
||||
public function clearCache(){
|
||||
$atemp = app()->getRootPath().'runtime/admin/temp/';
|
||||
$itemp = app()->getRootPath().'runtime/index/temp/';
|
||||
$cache = app()->getRootPath().'runtime/cache/';
|
||||
Files::delDirAndFile($atemp);
|
||||
Files::delDirAndFile($itemp);
|
||||
if(is_dir($cache) && Files::delDirAndFile($cache)){
|
||||
return json(['code'=>0,'msg'=>'清除缓存成功']);
|
||||
} else {
|
||||
return json(['code'=>-1,'msg'=>'清除缓存失败']);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use think\facade\Request;
|
||||
use think\facade\Db;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Session;
|
||||
use taoser\think\Auth;
|
||||
// 应用公共文件
|
||||
@ -75,7 +76,13 @@ if(!function_exists('getUserImg'))
|
||||
//根据文章分类ID查询分类名
|
||||
function getCateName($cate_ename)
|
||||
{
|
||||
return Db::name('cate')->where('ename',$cate_ename)->value('catename');
|
||||
$cname = Cache::get('cname');
|
||||
if(!$cname){
|
||||
$cname = Db::name('cate')->where('ename',$cate_ename)->value('catename');
|
||||
Cache::set('cname',$cname,3600);
|
||||
}
|
||||
return $cname;
|
||||
// return Db::name('cate')->where('ename',$cate_ename)->value('catename');
|
||||
}
|
||||
|
||||
|
||||
|
@ -14,6 +14,7 @@ use think\facade\Cache;
|
||||
use think\facade\View;
|
||||
use think\facade\Db;
|
||||
use taoser\think\Auth;
|
||||
use taoler\com\Files;
|
||||
|
||||
/**
|
||||
* 控制器基础类
|
||||
@ -234,5 +235,16 @@ abstract class AdminController
|
||||
}
|
||||
}
|
||||
}
|
||||
//清除缓存Cache
|
||||
public function clearData(){
|
||||
$dir = app()->getRootPath().'runtime/admin/temp';
|
||||
$cache = app()->getRootPath().'runtime/cache';
|
||||
if(is_dir($cache)){
|
||||
Files::delDirs($cache);
|
||||
}
|
||||
if(Files::delDirs($dir) ){
|
||||
return json(['code'=>0,'msg'=>'清除成功']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -226,7 +226,7 @@ abstract class BaseController
|
||||
protected function showNav()
|
||||
{
|
||||
//1.查询分类表获取所有分类
|
||||
$cateList = Db::name('cate')->where(['status'=>1,'delete_time'=>0])->order('sort','asc')->select();
|
||||
$cateList = Db::name('cate')->where(['status'=>1,'delete_time'=>0])->order('sort','asc')->cache(120)->select();
|
||||
|
||||
//2.将catelist变量赋给模板 公共模板nav.html
|
||||
View::assign('cateList',$cateList);
|
||||
@ -236,13 +236,10 @@ abstract class BaseController
|
||||
//显示用户
|
||||
protected function showUser()
|
||||
{
|
||||
$user['user_id'] = Session::get('user_id');
|
||||
//1.查询用户
|
||||
$user = Db::name('user')->field('id,name,nickname,user_img,sex,auth,city,email,sign,point')->where('id',$user['user_id'])->find();
|
||||
|
||||
//2.将User变量赋给模板 公共模板nav.html
|
||||
View::assign('user',$user);
|
||||
|
||||
$user = Db::name('user')->field('id,name,nickname,user_img,sex,auth,city,email,sign,point')->where('id',Session::get('user_id'))->cache(120)->find();
|
||||
//2.将User变量赋给模板 公共模板nav.html
|
||||
View::assign('user',$user);
|
||||
}
|
||||
|
||||
//显示网站设置
|
||||
|
@ -5,6 +5,7 @@ use app\common\controller\BaseController;
|
||||
use think\facade\View;
|
||||
use think\facade\Request;
|
||||
use think\facade\Db;
|
||||
use think\facade\Cache;
|
||||
use app\common\model\Article;
|
||||
use app\common\model\User;
|
||||
use app\common\model\Cate;
|
||||
@ -17,39 +18,63 @@ class Index extends BaseController
|
||||
{
|
||||
$types = input('type');
|
||||
//幻灯
|
||||
$sliders = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',1)->whereTime('slid_over','>=',time())->select();
|
||||
$sliders = Cache::get('slider');
|
||||
if(!$sliders){
|
||||
$sliders = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',1)->whereTime('slid_over','>=',time())->select();
|
||||
Cache::set('slider',$sliders,3600);
|
||||
}
|
||||
|
||||
//置顶文章
|
||||
$artTop = Article::field('id,title,cate_id,user_id,create_time,is_top')->where(['is_top'=>1,'status'=>1,'delete_time'=>0])->with([
|
||||
$artTop = Cache::get('arttop');
|
||||
if(!$artTop){
|
||||
$artTop = Article::field('id,title,cate_id,user_id,create_time,is_top')->where(['is_top'=>1,'status'=>1,'delete_time'=>0])->with([
|
||||
'cate' => function($query){
|
||||
$query->where('delete_time',0)->field('id,catename');
|
||||
},
|
||||
'user' => function($query){
|
||||
$query->field('id,name,nickname,user_img,area_id');
|
||||
}
|
||||
])->withCount(['comments'])->order('create_time','desc')->limit(5)->withCache(30)->select();
|
||||
])->withCount(['comments'])->order('create_time','desc')->limit(5)->select();
|
||||
Cache::set('arttop',$artTop,60);
|
||||
}
|
||||
|
||||
//首页文章显示15条
|
||||
$artList = Article::field('id,title,cate_id,user_id,create_time,is_hot')->with([
|
||||
//首页文章显示20条
|
||||
$artList = Cache::get('artlist');
|
||||
if(!$artList){
|
||||
$artList = Article::field('id,title,cate_id,user_id,create_time,is_hot')->with([
|
||||
'cate' => function($query){
|
||||
$query->where('delete_time',0)->field('id,catename');
|
||||
},
|
||||
'user' => function($query){
|
||||
$query->field('id,name,nickname,user_img,area_id');
|
||||
}
|
||||
])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(15)->withCache(30)->select();
|
||||
])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(20)->select();
|
||||
Cache::set('artlist',$artList,60);
|
||||
}
|
||||
|
||||
//热议文章
|
||||
$artHot = Article::field('id,title')->withCount('comments')->where(['status'=>1,'delete_time'=>0])->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->select();
|
||||
|
||||
//首页赞助
|
||||
$ad_index = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>3])->whereTime('slid_over','>=',time())->select();
|
||||
$ad_index = Cache::get('adindex');
|
||||
if(!$ad_index){
|
||||
$ad_index = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>3])->whereTime('slid_over','>=',time())->select();
|
||||
Cache::set('adindex',$ad_index,3600);
|
||||
}
|
||||
|
||||
//首页右栏
|
||||
$ad_comm = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>2])->whereTime('slid_over','>=',time())->select();
|
||||
$ad_comm = Cache::get('adcomm');
|
||||
if(!$ad_comm){
|
||||
$ad_comm = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>2])->whereTime('slid_over','>=',time())->select();
|
||||
Cache::set('adcomm',$ad_comm,3600);
|
||||
}
|
||||
|
||||
//友情链接
|
||||
$friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select();
|
||||
$friend_links = Cache::get('flinks');
|
||||
if(!$friend_links){
|
||||
$friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select();
|
||||
Cache::set('flinks',$friend_links,3600);
|
||||
}
|
||||
|
||||
$vs = [
|
||||
'slider' => $sliders,
|
||||
|
281
extend/taoler/com/Files.php
Normal file
281
extend/taoler/com/Files.php
Normal file
@ -0,0 +1,281 @@
|
||||
<?php
|
||||
|
||||
namespace taoler\com;
|
||||
use RecursiveIteratorIterator;
|
||||
use RecursiveDirectoryIterator;
|
||||
class Files
|
||||
{
|
||||
//创建文件夹及子文件夹
|
||||
public static function create_dirs($path)
|
||||
{
|
||||
if (!is_dir($path))
|
||||
{
|
||||
$directory_path = "";
|
||||
$directories = explode("/",$path);
|
||||
array_pop($directories);
|
||||
|
||||
foreach($directories as $directory)
|
||||
{
|
||||
$directory_path .= $directory."/";
|
||||
if (!is_dir($directory_path))
|
||||
{
|
||||
mkdir($directory_path);
|
||||
chmod($directory_path, 0777);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹及内容
|
||||
* @param $dirPath 所删除的目录
|
||||
* @param $nowDir 是否删除当前文件夹$dirPath true false
|
||||
*/
|
||||
public static function delDirAndFile( $dirPath, $nowDir=false )
|
||||
{
|
||||
if ( $handle = opendir($dirPath) ) {
|
||||
|
||||
while ( false !== ( $item = readdir( $handle ) ) ) {
|
||||
if ( $item != '.' && $item != '..' ) {
|
||||
$path = $dirPath.$item;
|
||||
if (is_dir($path)) {
|
||||
self::delDirAndFile($path.'/');
|
||||
rmdir($path.'/');
|
||||
} else {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir( $handle );
|
||||
//删除当前文件夹
|
||||
if($nowDir == true){
|
||||
if(rmdir($dirPath)){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文件夹$source下的文件和子文件夹下的内容到$dest下 升级+备份代码
|
||||
* @param $source
|
||||
* @param $dest
|
||||
* @param $ex 指定只复制$source下的目录,默认全复制
|
||||
*/
|
||||
public function copyDirs($source, $dest, $ex=array())
|
||||
{
|
||||
$count = count($ex);
|
||||
if (!file_exists($dest)) mkdir($dest);
|
||||
if($handle = opendir($source)){
|
||||
while (($file = readdir($handle)) !== false) {
|
||||
if (( $file != '.' ) && ( $file != '..' )) {
|
||||
if ( is_dir($source . $file) ) {
|
||||
//指定文件夹
|
||||
if($count != 0){
|
||||
if(in_array($file,$ex)){
|
||||
self::copyDirs($source . $file.'/', $dest . $file.'/');
|
||||
}
|
||||
} else {
|
||||
self::copyDirs($source . $file.'/', $dest . $file.'/');
|
||||
}
|
||||
} else {
|
||||
copy($source. $file, $dest . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($handle);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测目录并循环创建目录
|
||||
*
|
||||
* @param $catalogue
|
||||
*/
|
||||
public static function mkdirs($dir)
|
||||
{
|
||||
if (!file_exists($dir)) {
|
||||
self::mkdirs(dirname($dir));
|
||||
mkdir($dir, 0777);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $dir
|
||||
* @return bool
|
||||
* 删除文件以及目录
|
||||
*/
|
||||
public static function delDir($dir) {
|
||||
//先删除目录下的文件:
|
||||
// var_dump(is_dir($dir));
|
||||
// if(!is_dir($dir)){
|
||||
// return true;
|
||||
// }
|
||||
$dh=opendir($dir);
|
||||
while ($file=readdir($dh)) {
|
||||
if($file!="." && $file!="..") {
|
||||
$fullpath=$dir."/".$file;
|
||||
if(!is_dir($fullpath)) {
|
||||
unlink($fullpath);
|
||||
} else {
|
||||
self::delDir($fullpath);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($dh);
|
||||
//删除当前文件夹:
|
||||
if(rmdir($dir)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $source
|
||||
* @param $dest
|
||||
* 复制文件到指定文件
|
||||
*/
|
||||
public static function copyDir($source, $dest)
|
||||
{
|
||||
if (!is_dir($dest)) {
|
||||
self::mkdirs($dest, 0755, true);
|
||||
}
|
||||
foreach (
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::SELF_FIRST
|
||||
) as $item
|
||||
) {
|
||||
if ($item->isDir()) {
|
||||
$sontDir = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
|
||||
if (!is_dir($sontDir)) {
|
||||
self::mkdirs($sontDir, 0755, true);
|
||||
}
|
||||
} else {
|
||||
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*写入
|
||||
* @param string $type 1 为生成控制器 2 模型
|
||||
*/
|
||||
|
||||
public static function filePutContents($content,$filepath,$type){
|
||||
if($type==1){
|
||||
$str = file_get_contents($filepath);
|
||||
$parten = '/\s\/\*+start\*+\/(.*)\/\*+end\*+\//iUs';
|
||||
preg_match_all($parten,$str,$all);
|
||||
$ext_content = '';
|
||||
if($all[0]){
|
||||
foreach($all[0] as $key=>$val){
|
||||
$ext_content .= $val."\n\n";
|
||||
}
|
||||
}
|
||||
$content .= $ext_content."\n\n";
|
||||
$content .="}\n\n";
|
||||
}
|
||||
ob_start();
|
||||
echo $content;
|
||||
$_cache=ob_get_contents();
|
||||
ob_end_clean();
|
||||
if($_cache){
|
||||
$File = new \think\template\driver\File();
|
||||
$File->write($filepath, $_cache);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取文件夹大小
|
||||
*
|
||||
* @param string $dir 根文件夹路径
|
||||
* @return int
|
||||
*/
|
||||
public static function getDirSize($dir)
|
||||
{
|
||||
if(!is_dir($dir)){
|
||||
return false;
|
||||
}
|
||||
$handle = opendir($dir);
|
||||
$sizeResult = 0;
|
||||
while (false !== ($FolderOrFile = readdir($handle))) {
|
||||
if ($FolderOrFile != "." && $FolderOrFile != "..") {
|
||||
if (is_dir("$dir/$FolderOrFile")) {
|
||||
$sizeResult += self::getDirSize("$dir/$FolderOrFile");
|
||||
} else {
|
||||
$sizeResult += filesize("$dir/$FolderOrFile");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir($handle);
|
||||
return $sizeResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件
|
||||
*
|
||||
* @param $files
|
||||
*/
|
||||
public static function createFile($file,$content)
|
||||
{
|
||||
|
||||
$myfile = fopen($file, "w") or die("Unable to open file!");
|
||||
fwrite($myfile, $content);
|
||||
fclose($myfile);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 基于数组创建目录
|
||||
*
|
||||
* @param $files
|
||||
*/
|
||||
public static function createDirOrFiles($files)
|
||||
{
|
||||
foreach ($files as $key => $value) {
|
||||
if (substr($value, -1) == '/') {
|
||||
mkdir($value);
|
||||
} else {
|
||||
file_put_contents($value, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断文件或目录是否有写的权限
|
||||
public static function isWritable($file)
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == FALSE) {
|
||||
return is_writable($file);
|
||||
}
|
||||
if (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) {
|
||||
return FALSE;
|
||||
}
|
||||
fclose($fp);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入日志
|
||||
*
|
||||
* @param $path
|
||||
* @param $content
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function writeLog($path, $content)
|
||||
{
|
||||
self::mkdirs(dirname($path));
|
||||
return file_put_contents($path, "\r\n" . $content, FILE_APPEND);
|
||||
}
|
||||
}
|
@ -451,6 +451,33 @@ layui.define('view', function(exports){
|
||||
}
|
||||
}
|
||||
|
||||
//清除缓存
|
||||
,clearcache: function(){
|
||||
loading = layer.load(2, {
|
||||
shade: [0.2, '#000']
|
||||
});
|
||||
admin.req({
|
||||
type: "post"
|
||||
,url: '/admin/Admin/clearCache'
|
||||
,success: function(data){
|
||||
if (data.code == 0) {
|
||||
layer.close(loading);
|
||||
layer.msg(data.msg,{
|
||||
icon:6,
|
||||
time:2000
|
||||
});
|
||||
} else {
|
||||
layer.close(loading);
|
||||
layer.open({
|
||||
content:data.msg,
|
||||
icon:5,
|
||||
anim:6
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//弹出关于面板
|
||||
,about: function(){
|
||||
admin.popupRight({
|
||||
|
@ -46,7 +46,7 @@
|
||||
</a>
|
||||
</li>
|
||||
<li class="layui-nav-item layui-hide-xs" lay-unselect>
|
||||
<a href="javascript:;" layadmin-event="#">
|
||||
<a href="javascript:;" layadmin-event="clearcache">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
</a>
|
||||
</li>
|
||||
|
@ -1,7 +1,6 @@
|
||||
|
||||
{extend name="public:base" /}
|
||||
|
||||
{block name="title"}{$sysInfo.webname} - {$sysInfo.webtitle}{/block}
|
||||
{block name="title"}{$sysInfo.webname}-{$sysInfo.webtitle}{/block}
|
||||
{block name="keywords"}{$sysInfo.keywords}{/block}
|
||||
{block name="description"} {$sysInfo.descript}{/block}
|
||||
{block name="column"}{include file="public/column" /}{/block}
|
||||
@ -71,7 +70,7 @@
|
||||
{volist name="artList" id="art" empty="还没有帖子"}
|
||||
<li>
|
||||
<a href="{:url('user/home',['id'=>$art.user_id])}" class="fly-avatar">
|
||||
<img src="{$art.user_id|getUserImg}" alt="{$art.user.name}">
|
||||
<img src="{$art.user.user_img}" alt="{$art.user.name}">
|
||||
</a>
|
||||
<h2>
|
||||
<a class="layui-badge">{$art.cate.catename}</a>
|
||||
@ -338,8 +337,8 @@
|
||||
layui.cache.page = 'index';
|
||||
layui.cache.user = {
|
||||
username: '{$user.name??'游客'}'
|
||||
,uid: {$user.id ? 168*$user.id : -1}
|
||||
,avatar: '{if condition="$user['user_img'] neq ''"}{$user['user_img']}{else /}/static/res/images/avatar/00.jpg{/if}'
|
||||
,uid: {$user.id ? $user.id : -1}
|
||||
,avatar: '{$user.name? $user['user_img']:'/static/res/images/avatar/00.jpg'}'
|
||||
,experience: 83
|
||||
,sex: '{if condition="$user['sex'] eq 0"}男{else/}女{/if}'
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user