静态缓存
保存在磁盘上的静态文件,用PHP生成数据放入静态文件中
PHP操作缓存
1.生成缓存
2.获取缓存
3.删除缓存<?php
class File {
private $_fir;
const EXT = '.txt';
public function __construct() {
$this->_dir = dirname(__FILE__) . '/files/';//当前文件目录下的files
}
/**
* key 文件名
* value 值
* path 路径
*/
public function cacheData($key, $value = '', $path = '') {
$filename = $this->_dir . $path . $key . self::EXT;
if($value !== '') { // 将value写入缓存
if(is_null($value)) {
//删除缓存
return @unlink($filename);
}
$dir = dirname($filename);
if(!is_dir($dir)) {
mkdir($dir, 0777);//创建目录
}
return file_put_contents($filename, json_encode($value));
}
if(!is_file($filename)) {
return false;
} else {
return json_decode(file_get_contents($filename), true);
}
}
}
Memcached , Redis
1.Memcached 和 Redis都是用来管理数据的
2.他们数据都是存放在内存里的
3.Redis可以定期将数据备份到磁盘(持久化)
4.Memcached只是简单的key/value缓存
5.Redis不仅仅支持简单的k/v类型的数据,同时还提供list, set, hash等数据结构的存储
Redis数据操作
1.开启redis客户端
2.设置缓存值 – set index-mk-cache ‘数据’
3.获取缓存数据 – get index-mk-cache
4.设置过期时间 – setex key 10 ‘cache’
5.删除缓存 – del key
php操作Redis
1.安装phpredis扩展
2.php链接redis服务-connect(127.0.0.1, 6379)
3.set 设置缓存
4.get 获取缓存<?php
//Redis
$redis = new Redis();
$redis->connect('127.0.0.1', '6379');//地址,端口号
$redis->set('singwa', 123);//设置值
var_dump($redis->get('singwal'));//获取值,不存在返回false
$redis->setex('singwa2', 15, 'sdahdahdadad');//15s过期
php操作memcache
1.安装memcache扩展
2.链接服务-connect(‘memcache_host’, 11211);
3.set 设置缓存
4.get 获取缓存