- 浏览: 171951 次
- 来自: 深圳
文章分类
最新评论
-
GGGGeek:
MySQL5.7.24varchar(65532)报错:107 ...
int(11)最大长度是多少,MySQL中varchar最大长度是多少?
转自:http://blog.csdn.net/heiyilvzhe/article/details/5979730
需求
1. 为数据库连接类读数据增加缓存处理,缓存方式可以为文件、Memory Cache
先只对Memory Cache实现
2. 对现有代码不进行大的改动,方便移植
3. 数据库连接类可扩充其他功能。
客户端调用
以前的方法
$result = $db_bbs->query_memcache($sql_str);
if(!$result) {
$result = $db_bbs->get_results($sql_str);
}
现在的只要一行$db_bbs->get_results($sql_str)。
如果写个文件缓存类的实现,生成文件缓存也像操作数据库一样都用$db_bbs->get_results($sql_str);
就是说操作memory cache、文件和真正的数据库没区别,对于调用的程序是透明的。
使用装饰模式可以将对memory cache、和文件的操作独立出来。
//----------------------------------------我是分割线-------------------------------------------------
旧的数据库连接类和支持缓存的连接类都实现以下接口
//存储查询写入接口
interface interface_DB {
public function get_results($sql, $oMethod = 'A');
public function query($sql);
public function get_row($sql , $oMethod = 'A');
public function get_var($sql);
}
支持不同缓存的连接类可以对get_results等方法进行各自的实现。
//----------------------------------------我是分割线-------------------------------------------------
数据库连接类使用工厂模式构造
下面是具体代码
<?php
require_once(DBSTORE."DB_Base.php");
class StoreFactory{
static function create($db,$classname="") {
if(!$classname){
return $db;
}
if (!class_exists($classname)) {
if(include_once(DBSTORE.$classname.'.php')){
return new $classname($db);
}else{
throw new Exception ($classname .'file not found');
}
}
}
}
?>
//----------------------------------------我是分割线-------------------------------------------------
支持缓存的数据库连接类
构造函数
public function __construct(DB_Base $db){ #初始化
$this->MEM_PRE_KEY = $_SERVER['SERVER_NAME'];
$this->db = $db;
$this->mem = new Memcache;
foreach ($this->IP as $key=>$value)
{
$this->mem->addServer($value,11211,false);
}
}
get_results 实现方法
public function get_results($sql,$oMethod="A"){
if (preg_match('/(^select)/sim', $sql)) {
$this->content = $this->get_content($sql);
if ($this->content){
$this->content =unserialize($this->content);
}
if(!$this->content) {
$this->content = $this->db->get_results($sql,$oMethod);
if ($this->content){
$this->set_content($sql,serialize($this->content),$this->limit_time);
}
}}else{
$this->get_results_nocache($sql,$oMethod);
}
return $this->content;
}
没有缓存或者缓存过期时调用的方法
protected function get_results_nocache($sql,$oMethod){
$this->content = $this->db->get_results($sql,$oMethod);
return $this->content;
}
//----------------------------------------我是分割线-------------------------------------------------
<?php
define('DBSTORE',"D:/projects/dp/decorator/store/");
require_once(DBSTORE."class_storefactory.php");
$db = StoreFactory::create($DB_localhost,"MemoryStore");
$db->limit_time=10;
$r_old = $DB_localhost->get_results("select phraseid,varname,text from phrase phraseid limit 0,1#yiduo ");
$r = $db->get_results("select phraseid,varname,text from phrase phraseid limit 0,1#yiduo ");
if($r_old==$r) {
print_r("OK");
}
?>
//----------------------------------------我是分割线-------------------------------------------------
利用装饰模式可以为数据库连接类扩展功能
如果你写个数据库日志类DB_LOG
可以这样建一个对象
$db = StoreFactory::create($DB_MemoryStore,"DB_LOG");
那$db 类就有了数据库日志记录功能,同时还有memory cache的功能
数据库日志记录功能本身不和DB_MemoryStore类耦合,DB_MemoryStore类的代码不会特别庞大
发表评论
-
PHP+MYSQL大小写问题
2014-11-21 11:33 1593[PHP]一、大小写敏感 1. 变量名区分大小写 所有变 ... -
php基础知识之 ---- echo、print_r、var_dump、调试输出
2014-04-29 18:19 0。。。。。。。。 -
php基础知识之 ---- 过滤、防sql注入
2014-04-29 18:13 0。。。。。。。 -
php基础知识之 ---- 错误处理、异常、错误日志
2014-04-29 18:11 0。。。。。。。。 -
php基础知识之 ---- include 、 require、常量应用
2014-04-29 18:09 0。。。。。。。 -
php基础知识之 ---- 发邮件
2014-04-29 18:05 0。。。。。。 -
php基础知识之 ---- 日期时间
2014-04-29 18:04 0。。。。。。。 -
php基础知识之 ---- 权限及web访问权限
2014-04-29 18:01 0。。。。。 -
php基础知识之 ---- 文件操作
2014-04-29 18:00 0。。。。。。。。。 -
php基础知识之 ---- Cookie
2014-04-29 17:58 0......... -
php基础知识之 ---- Session
2014-04-29 17:58 0..... -
php基础知识之 ---- 数组、foreach
2014-04-29 17:56 0...... -
php基础知识之 ---- 运算符、大数据取模/求余计算得到负数
2014-04-29 17:54 0在写代码的时候,发现对大数字进行%求余(取模)的时候,获得的 ... -
php基础知识之 ---- 三元运算、isset()、empty()
2014-04-29 17:51 0....... -
php基础知识之 ---- 字符串、变量、单引号、双引号
2014-04-29 17:44 0。。。。。。 -
带BOM的utf-8,用json_decode() 返回null的问题 --- 超过3个bom字符
2014-03-17 12:01 2595PHP中file_get_contents函数获取URL文件 ... -
Parse error: syntax error, unexpected '}'
2014-02-12 17:53 1545Parse error: syntax error, une ... -
超实用的PHP代码片段
2013-11-22 15:48 895一、查看邮件是否已被阅读当你在发送邮件时,你或许很想知道该 ... -
8 个PHP功能开发
2013-10-28 17:10 10311、传递任意数量的函数参数 我们在.NET或者JA ... -
三个超好用的PHP加密解密函数
2013-10-28 16:24 4168三个超好用的PHP加密解密函数,貌似是discuz里的…使用 ...
相关推荐
还在测试,这几天完善一下
java 的内存管理,速度是数据的n倍。 大数据可以学习一哈
主要讲解x86架构下的cache的怎么工作原理和怎么实现等,说的很详细,水平很高的,但是要有相应的知识才能看明白,作者是王齐,
cache memory, cache line, write through and write back. Average memory access time = Hit time + Miss rate x Miss penalty
Cache-Memory处于Memory Hierarch的最顶端,其下是主存储器和外部存储器,合理使用Cache是降低存储器访问时间的有效途径。介绍了的Cache的工作原理组成结构等。
详细介绍存储器系统,包括cache,dram以及磁盘。英文版
A brief introduction for cache memory and point out the performance increment effectively.
介绍CPU硬件cache的一本书,国内写的较好的资料
英文版《A primer on memory consistency and cache coherence》带标签,讲解内存和缓存一致性的经典的书。
просроченного кеша каждые 10 минутcache := memorycache.New(5 * time.Minute, 10 * time.Minute)用 // Установить кеш с ключем "myKey" и временем
Memory Systems: Cache, DRAM, Disk is the fi rst book that takes on the whole hierarchy in a way that is consistent, covers the complete memory hierarchy, and treats each aspect in signifi cant detail....
存储系统――缓存.DRAM.磁盘.(Memory.Systems-Cache.DRAM.Disk).Bruce.Jacob pdf文档)
Memory Systems - Cache, DRAM, Disk.part2
Memory Systems - Cache, DRAM, Disk.part1
A Primer on Memory Consistency and Cache Coherence,主要介绍了Memory同一性和cache一致性的定义以及相关模型和协议。例如SC模型,TSO模型,松散序模型和cache一致性的两种协议:snoopy协议和目录一致性协议。...
Memory Systems - Cache, DRAM, Disk
A Primer on Memory Consistency and Cache Coherence Daniel J. Sorin Mark D. Hill David A. Wood Morgan & Claypool Publishers
Memory Systems - Cache, DRAM, Disk
介绍存储结构的好书,国外非常优秀的参考书。(由于整本书太大,分成两部分下载。谢谢喜欢!)0
一种内存数据库到文件数据库的数据同步方法,包括:A、内存数据库将事务日志记录写入日志缓存区;B、定义内存数据库和文件数据库数据表的对应关系;C、根据事务日志刷新进程,异步地将所述日志缓存区中的所述事务日志...