0

0

[php]标记投射和工作单元

php中文网

php中文网

发布时间:2016-06-13 11:38:04

|

1004人浏览过

|

来源于php中文网

原创

[php]标记映射和工作单元
    标记映射

        系统中可能存在两个值相同,但又不是同一个引用的对象,这样的重复对象可能是从数据库中读出来的,这样就造成了不必要的查询。

        标记映射是一个类ObjectWatcher,它负责管理进程中的领域对象,以保证进程中不出现重复对象。

        标记映射可以防止重新读取数据库查询数据,只有当ObjectWatcher类中不存在标记映射对应的对象时才去查询数据库。这样就保证了在一个进程中,一条数据只对应一个对象。

        代码很容易懂,都是一些存取数组值的操作。

        ObjectWatcher代码:

立即学习PHP免费学习笔记(深入)”;

namespace demo\domain;use \demo\domain\DomainObject;/** *  标记映射 */class ObjectWatcher {	private static $instance;	// 标记映射	private $all = array();		private function __construct() {			}		public  static function getInstance() {		if (!isset(self::$instance)) {			self::$instance = new self();		}				return self::$instance;	}		/**	 * 获得对象对应的键值	 * @param DomainObject $obj	 */	public function getGobalKey(DomainObject $obj) {		$key = get_class($obj) . '_' . $obj->getId();		return $key;	}		/**	 * 添加到all	 * @param DomainObject $obj	 */	public static function add(DomainObject $obj) {		$instance = self::getInstance();		$key = $instance->getGobalKey($obj);		$instance->all[$key] = $obj;	}		/**	 * 从all中删除	 * @param DomainObject $obj	 */	public static function delete(DomainObject $obj) {		$instance = self::getInstance();		$key = $instance->getGobalKey($obj);		unset($instance->all[$key]);	}		/**	 * 判断标记是否存在	 * @param string $className	 * @param int $id	 */	public  static function exists($className, $id) {		$instance = self::getInstance();		$key = "{$className}_{$id}";		if (isset($instance->all[$key])) {			return $instance->all[$key];		}				return null;	}}
        那么在哪里做标记呢?当然是生成查询对象的地方,分别有Mapper::find()、Mapper::insert()、Mapper::createObject()。 Mapper中新增加了addToMap()和getFromMap()。(其它方法没有改变,所以看以忽略吧。)

        Mapper代码:

namespace demo\mapper;use \demo\base\AppException;use \demo\base\ApplicationRegistry;use \demo\domain\DomainObject;use \demo\domain\ObjectWatcher;/** * Mapper */abstract  class Mapper {	// PDO	protected static $PDO;	// config	protected static  $dsn, $dbUserName, $dbPassword;	// PDO选项	protected static $options = array(    	\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',    	\PDO::ATTR_ERRMODE,    	\PDO::ERRMODE_EXCEPTION,	); 		public function __construct() {		if (!isset(self::$PDO)) {			// ApplicationRegistry获取数据库连接信息			$appRegistry = ApplicationRegistry::getInstance();			self::$dsn = $appRegistry->getDsn();			self::$dbUserName = $appRegistry->getDbUserName();			self::$dbPassword = $appRegistry->getDbPassword();						if (!self::$dsn || !self::$dbUserName || !self::$dbPassword) {				throw  new AppException('Mapper init failed!');			}						self::$PDO = new \PDO(self::$dsn, self::$dbUserName, self::$dbPassword, self::$options);		}	}		/**	 * 查找指定ID	 * @param int $id	 */	public function findById($id) {		// 从ObjectWatcher中获取		$obj = $this->getFromMap($id);		if (!is_null($obj)) {			return $obj;		}				$pStmt = $this->getSelectStmt();				$pStmt->execute(array($id));			$data = $pStmt->fetch();				$pStmt->closeCursor();				if (!is_array($data) || !isset($data['id'])) {			return $obj;		}		$obj = $this->createObject($data);				return $obj;	}		/**	 * 返回Collection	 */	public function findAll() {		$pStmt = $this->getSelectAllStmt();		$pStmt->execute(array());		$raws = $pStmt->fetchAll(\PDO::FETCH_ASSOC);		$collection = $this->getCollection($raws);				return $collection;	}		/**	 * 插入数据	 * @param \demo\domain\DomainObject $obj	 */	public function insert(DomainObject $obj) {		$flag = $this->doInsert($obj);		// 保存或者更新ObjectWatcher的$all[$key]的对象		$this->addToMap($obj);		return $flag;	}		/**	 *  更新对象	 * @param \demo\domain\DomainObject $obj	 */	public  function update(\demo\domain\DomainObject $obj) {		$flag = $this->doUpdate($obj);				return $flag;	}		/**	 * 删除指定ID	 * @param int $id	 */	public function deleteById($id) {		$pStmt = $this->getDeleteStmt();		$flag = $pStmt->execute(array($id));				return $flag;	}		/**	 * 生成一个$data中值属性的对象	 * @param array $data	 */	public function createObject(array $data) {		// 从ObjectWatcher中获取		$obj = $this->getFromMap($data['id']);		if (!is_null($obj)) {			return $obj;		}				// 创建对象		$obj = $this->doCreateObject($data);		// 添加到ObjectWatcher		$this->addToMap($obj);				return $obj;	}		/**	 * 返回对应key标记的对象	 * @param int $id	 */	private function getFromMap($id) {		return ObjectWatcher::exists($this->getTargetClass(), $id);	}		/**	 * 添加对象到标记映射ObjectWatcher类	 * @param DomainObject $obj	 */	private function addToMap(DomainObject $obj) {		return ObjectWatcher::add($obj);	}		/**	 * 返回子类Collection	 * @param array $raw	 */	public function getCollection(array $raws) {		return $this->getFactory()->getCollection($raws);	}		/**	 * 返回子类持久化工厂对象	 */	public function getFactory() {		return PersistanceFactory::getFactory($this->getTargetClass());			}		protected abstract function doInsert(\demo\domain\DomainObject $obj);		protected abstract function doCreateObject(array $data);		protected abstract function getSelectStmt();		protected abstract function getSelectAllStmt();		protected abstract function doUpdate(\demo\domain\DomainObject $obj);		protected abstract function getDeleteStmt();	protected abstract function getTargetClass();}

        大部分代码是之前的,修改的只是一小部分。下面图一张:

                                                               

        现在,当Mapper从数据库中取出的数据映射成的对象都被标记到ObjectWatcher了,而且不需要对对象手动操作标记到ObjectWatcher,Mapper就已经帮你完成了。这样带来的好处是可以减少对数据库的操作和新对象的创建,比如find、createObject。但这也许可能带来问题,如果你的程序需要并发处理数据,那么被标记的对象数据就可能不一致了,你在这个时候可能需要对数据加锁。  


    工作单元

          有些时候,我们可能没有改变数据的任何值却向数据库多次保存该数据,这当然是不必要的吧。工作单元可以使你只保存那些需要的对象。工作单元可以在一次请求即将结束时,把在这次请求中发生变化的对象保存到数据库中。一次请求的最后是在控制器(Controller)调用完Command和View之后,那么我们就可以在这里让工作单元执行任务。  

        标记映射的作用是在处理过程开始时向数据库加载不必要的对象,而工作单元则是在处理过程之后防止不必要的对象保存到数据库中。这两个工作方式就像是互补的。

        为了判断哪些数据库的操作是必要的,那就需要跟踪与对象相关的各种事件(比如:setter()重新设置了对象的属性值)。跟踪工作当然最好放在被跟踪的对象中。

        修改过的ObjectWatcher类:

Lenso.ai
Lenso.ai

AI反向图像搜索

下载
namespace demo\domain;use \demo\domain\DomainObject;/** *  标记映射 */class ObjectWatcher {	private static $instance;	// 标记映射	private $all = array();	// 保存新建对象	private $new = array();	// 保存被修改过的对象(“脏对象”)	private $dirty = array();	// 保存删除对象	private $delete = array();		private function __construct() {			}		public  static function getInstance() {		if (!isset(self::$instance)) {			self::$instance = new self();		}				return self::$instance;	}		/**	 * 获得对象对应的键值	 * @param DomainObject $obj	 */	public function getGobalKey(DomainObject $obj) {		$key = get_class($obj) . '_' . $obj->getId();		return $key;	}		/**	 * 添加到all	 * @param DomainObject $obj	 */	public static function add(DomainObject $obj) {		$instance = self::getInstance();		$key = $instance->getGobalKey($obj);		$instance->all[$key] = $obj;	}		/**	 * 从all中删除	 * @param DomainObject $obj	 */	public static function delete(DomainObject $obj) {		$instance = self::getInstance();		$key = $instance->getGobalKey($obj);		unset($instance->all[$key]);	}		/**	 * 添加到new	 * @param DomianObject $obj	 */	public static function addNew(DomainObject $obj) {		$instance = self::getInstance();		$instance->new[] = $obj;	}		/**	 * 添加到dirty	 * @param DomianObject $obj	 */	public static function addDirty(DomainObject $obj) {		$instance = self::getInstance();		if (!in_array($obj, $instance->dirty, true)) {			$instance->dirty[$instance->getGobalKey($obj)] = $obj;		}	}		/**	 * 添加到delete	 * @param DomainObject $obj	 */	public static function addDelete(DomainObject $obj) {		$instance = self::getInstance();		$instance->delete[$instance->getGobalKey($obj)] = $obj;	}		/**	 * 清除标记dirty new delete	 * @param DomainObject $obj	 */	public static function addClean(DomainObject $obj) {		$instance = self::getInstance();		// unset删除保存的对象		unset($instance->dirty[$instance->getGobalKey($obj)]);		unset($instance->delete[$instance->getGobalKey($obj)]);		// 删除new中的对象		$instance->new = array_filter($instance->new, function($a) use ($obj) {			return !($a === $obj);		});	}		/**	 * 判断标记是否存在	 * @param string $className	 * @param int $id	 */	public  static function exists($className, $id) {		$instance = self::getInstance();		$key = "{$className}_{$id}";		if (isset($instance->all[$key])) {			return $instance->all[$key];		}				return null;	}		/**	 * 对new dirty delete 中的标记对象执行操作	 */	public function performOperations() {		$instance = self::getInstance();				// new		foreach ($instance->new as $obj) {			$obj->finder()->insert($obj);		}				// dirty		foreach ($instance->dirty as $obj) {			$obj->finder()->update($obj);		}				// delete		foreach ($instance->delete as $obj) {			$obj->finder()->delete($obj);		}				$this->new = array();		$this->dirty = array();		$this->delete = array();	}}
        ObjectWatcher依然是标记映射,只是在这里增加了跟踪系统中对象的变化的功能。ObjectWatcher类提供了查找、删除、添加对象到数据库的机制。

        由于ObjectWatcher的操作上对对象的操作,所以由这些对象自己来来执行ObjectWatcher是很适合的。

        修改过的DomainObject类(markNew()、markDirty()、markDelete()、markClean()):

namespace demo\domain;use \demo\domain\HelperFactory;use \demo\domain\ObjectWatcher;/** * 领域模型抽象基类 */abstract class DomainObject {	protected  $id = -1;		public function __construct($id = null) {		if (is_null($id)) {			// 标记为new 新建			$this->markNew();		} else {			$this->id = $id;		}	}		public function getId() {		return $this->id;	}		public function setId($id) {		$this->id = $id;		$this->markDirty();	}		public function markNew() {		ObjectWatcher::addNew($this);	}		public function markDirty() {		ObjectWatcher::addDirty($this);	}		public function markDeleted() {		ObjectWatcher::addDelete($this);	}		public function markClean() {		ObjectWatcher::addClean($this);	}		public static function getCollection($type) {		return HelperFactory::getCollection($type);	}		public function collection() {		return self::getCollection(get_class($this));	}		public static function getFinder($type) {		return HelperFactory::getFinder($type);	}		public function finder() {		return self::getFinder(get_class($this));	}}

        DomainObject和ObjectWatcher的关系图一张:
                                                     

         修改过的Mapper(和上面相同部分略去了,太占位子了):

/** * Mapper */abstract  class Mapper {	//...		/**	 * 查找指定ID	 * @param int $id	 */	public function findById($id) {		// 从ObjectWatcher中获取		$obj = $this->getFromMap($id);		if (!is_null($obj)) {			return $obj;		}				$pStmt = $this->getSelectStmt();				$pStmt->execute(array($id));			$data = $pStmt->fetch();				$pStmt->closeCursor();				if (!is_array($data) || !isset($data['id'])) {			return $obj;		}		$obj = $this->createObject($data);				return $obj;	}			/**	 * 插入数据	 * @param \demo\domain\DomainObject $obj	 */	public function insert(DomainObject $obj) {		$flag = $this->doInsert($obj);		// 保存或者更新ObjectWatcher的$all[$key]的对象		$this->addToMap($obj);		$obj->markClean();		// 调试用的				echo 'insert :' . get_class($obj) . '_' . $obj->getName() . '_' . $obj->getId() .'<br/>';			return $flag;	}		/**	 *  更新对象	 * @param \demo\domain\DomainObject $obj	 */	public  function update(\demo\domain\DomainObject $obj) {		$flag = $this->doUpdate($obj);		$obj->markClean();		// 调试用的				echo 'update :' . get_class($obj) . '_' . $obj->getName() . '_' . $obj->getId() .'<br/>';					return $flag;	}			/**	 * 生成一个$data中值属性的对象	 * @param array $data	 */	public function createObject(array $data) {		// 从ObjectWatcher中获取		$obj = $this->getFromMap($data['id']);		if (!is_null($obj)) {			return $obj;		}				// 创建对象		$obj = $this->doCreateObject($data);		// 添加到ObjectWatcher		$this->addToMap($obj);		// 清除new标记        ObjectWatcher::addClean($obj);				return $obj;	}		//...}
        可以看到Mapper中修改的部分都是有改变对象的事件发生,即find()、update()、insert()、delete()。

        对象的变化都能被跟踪到了,那么应该在哪里处理这些变化过的对象(“脏数据”)呢?上面说到了,应该在一次请求即将完成的时候。

        一次请求即将结束时,Controller中调用工作单元(同样省略了没改变的代码):

namespace demo\controller;/** * Controller */class Controller {	// ...		private function handleReuqest() {		$request = new \demo\controller\Request();		$appController = \demo\base\ApplicationRegistry::getInstance()->getAppController();		// 执行完所有Command,有可能存在forward		while ($cmd = $appController->getCommand($request)) {			// var_dump($cmd);			$cmd->execute($request);			// 把当前Command设为已执行过			$request->setLastCommand($cmd);		}		// 工作单元执行任务		ObjectWatcher::getInstance()->performOperations();		// 获取视图		$view = $appController->getView($request);		// 显示视图		$this->invokeView($view);	}		// ...}
        ObjectWatcher::getInstance()->performOperations()


        好的,现在来个使用例子吧:

namespace demo\command;use demo\domain\Classroom;use demo\base\ApplicationRegistry;use demo\domain\ObjectWatcher;use demo\domain\HelperFactory;class Test extends Command {	protected function doExecute(\demo\controller\Request $request) {		$crMapper = HelperFactory::getFinder('demo\domain\Classroom');				// 新创建的对象 markNew()		$crA = new Classroom();		$crA->setName('四年(3)班');				// 修改后的“脏”数据		$crB = $crMapper->findById(1);		$crB->setName("五年(2)班");	}}

        输入的Url:

localhost/demo/runner.php?cmd=Test
      输出结果与预期的一样:
insert :demo\domain\Classroom_四年(3)班_58update :demo\domain\Classroom_五年(2)班_1

        

        现在对领域对象的管理有了较大的改进了。还有,我们使用模式的目的是提高效率,而不是降低效率。
        


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

616

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

194

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

91

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

20

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

54

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

29

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

598

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

56

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
前端系列快速入门课程
前端系列快速入门课程

共4课时 | 0.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号