php文件缓存类用法实例分析
作者:邪云子 发布时间:2023-08-17 16:26:44
标签:php,文件,缓存类
本文实例讲述了php文件缓存类用法。分享给大家供大家参考。具体如下:
<?php
/**
* 简单的文件缓存类
*
*/
class XZCache{
// default cache time one hour
var $cache_time = 3600;
// default cache dir
var $cache_dir = './cache';
public function __construct($cache_dir=null, $cache_time=null){
$this->cache_dir = isset($cache_dir) ? $cache_dir : $this->cache_dir;
$this->cache_time = isset($cache_time) ? $cache_time : $this->cache_time;
}
public function saveCache ($key, $value){
if (is_dir($this->cache_dir)){
$cache_file = $this->cache_dir . '/xzcache_' . md5($key);
$timedif = @(time() - filemtime($cache_file));
if ($timedif >= $this->cache_time) {
// cached file is too old, create new
$serialized = serialize($value);
if ($f = @fopen($cache_file, 'w')) {
fwrite ($f, $serialized, strlen($serialized));
fclose($f);
}
}
$result = 1;
}else{
echo "Error:dir is not exist.";
$result = 0;
}
return $result;
}
/**
* @return array
* 0 no cache
* 1 cached
* 2 overdue
*/
public function getCache ($key) {
$cache_file = $this->cache_dir . '/xzcache_' . md5($key);
if (is_dir($this->cache_dir) && is_file($cache_file)) {
$timedif = @(time() - filemtime($cache_file));
if ($timedif >= $this->cache_time) {
$result['cached'] = 2;
}else{
// cached file is fresh enough, return cached array
$result['value'] = unserialize(file_get_contents($cache_file));
$result['cached'] = 1;
}
}else {
echo "Error:no cache";
$result['cached'] = 0;
}
return $result;
}
} //end of class
用法示例如下:
$cache = new XZCache();
$key = 'global';
$value = $GLOBALS;
$cache->saveCache($key, $value);
$result = $cache->getCache($key);
var_dump($result);
希望本文所述对大家的php程序设计有所帮助。


猜你喜欢
- 代码如下#encoding:utf-8import requestsfrom lxml import etreeimport xlwtimp
- 查询数据指从数据库中获取所需要的数据。查询数据是数据库操作中最常用,也是最重要的操作。用户可以根据自己对数据的需求,使用不同的查询方式。通过
- 之前跟一些小伙伴有个讨论:大概就是很多跟数据打交道的朋友都面对过很复杂的excel公式,有时嵌套层数特别多,肉眼观看很容易蒙圈。有了这样的需
- 本文实例讲述了Python 多线程,threading模块,创建子线程的两种方式。分享给大家供大家参考,具体如下:GIL(全局解释器锁)是C
- 可以使用python中的sys模块的getrefcount()方法来获取对象引用的个数。具体可以看以下的实例:import sys # 首先
- 目录前言 字符串都有哪些操作?第一类 判断识别字符串第二类 字符串编辑的操作第三类:字符串跟字节串的互转总结前言今天我们说了字符串
- 在网上down了个web项目,在 IntelliJ IDEA 这个编辑器里面跑起来,但是发现domain文件夹下的xml文件都报如下的红色提
- 本文实例为大家分享了python实现发送邮件功能的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*- # Au
- 本文实例讲述了PHP实现的curl批量请求操作。分享给大家供大家参考,具体如下:<?php$ch = array();$res = a
- 方法一:def commaSpiltList(self, listData): listData = list(listData) strs
- 本文实例讲述了Python sqlite3事务处理方法。分享给大家供大家参考,具体如下:sqlite3事务总结:在connect()中不传入
- 这篇文章主要介绍了python中的Elasticsearch操作汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 冒泡排序冒泡排序(英语:Bubble Sort)是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们
- 本文实例为大家分享了抖音代码舞python制作代码,供大家参考,具体内容如下一、效果图二、转换代码from img import Image
- 今天在看文档的时候,发现pytorch 的conv操作不是很明白,于是有了一下记录首先提出两个问题:1.输入图片是单通道情况下的filter
- 如下所示:import matplotlib.pyplot as pltimport numpy as npimport mathdef g
- 由于想使用python用训练好的caffemodel来对很多图片进行批处理分类,学习过程中,碰到了argsort函数,因此去查了相关文献,也
- 为什么我写ASP分页教程要提到AJAX呢,因为我们要多练习一下编程过程中,结构化的重要性. 再加上很多朋友对分页感到很高深,所以一直都不敢去
- 测试过程如下:create table sales as select * f
- append(),extend(), insert()都是列表操作中常用的插入函数。其中前两个均接收一个参数,并插入到列表尾部。最后一个接收