-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSmartDaoWorker.php
More file actions
180 lines (142 loc) · 3.92 KB
/
SmartDaoWorker.php
File metadata and controls
180 lines (142 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/***************************************************************************
* Copyright (C) 2005-2008 by Konstantin V. Arkhipov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/
namespace OnPHP\Main\DAO\Worker;
use OnPHP\Core\Base\Assert;
use OnPHP\Core\Base\Identifiable;
use OnPHP\Core\Cache\Cache;
use OnPHP\Core\Cache\SemaphorePool;
use OnPHP\Core\OSQL\SelectQuery;
use OnPHP\Main\DAO\GenericDAO;
use OnPHP\Main\DAO\Uncacher\UncacherSmartDaoWorkerLists;
/**
* Transparent caching DAO worker.
*
* @see CommonDaoWorker for manual-caching one.
* @see VoodooDaoWorker for greedy though non-blocking brother.
*
* @ingroup DAO
**/
final class SmartDaoWorker extends TransparentDaoWorker
{
private $indexKey = null;
public function __construct(GenericDAO $dao)
{
parent::__construct($dao);
$this->indexKey =
$this->watermark
.$this->className
.self::SUFFIX_INDEX;
}
/// cachers
//@{
protected function cacheByQuery(
SelectQuery $query,
/* Identifiable */ $object,
$expires = Cache::EXPIRES_FOREVER
)
{
$semKey = $this->keyToInt($this->indexKey);
$key = $this->makeQueryKey($query, self::SUFFIX_QUERY);
$pool = SemaphorePool::me();
if ($pool->get($semKey)) {
$this->syncMap($key);
Cache::me()->mark($this->className)->
add($key, $object, $expires);
$pool->free($semKey);
}
return $object;
}
protected function cacheListByQuery(
SelectQuery $query,
/* array || Cache::NOT_FOUND */ $array
)
{
if ($array !== Cache::NOT_FOUND) {
Assert::isArray($array);
Assert::isTrue(current($array) instanceof Identifiable);
}
$cache = Cache::me();
$listKey = $this->makeQueryKey($query, self::SUFFIX_LIST);
$semKey = $this->keyToInt($this->indexKey);
$pool = SemaphorePool::me();
if ($pool->get($semKey)) {
$this->syncMap($listKey);
$cache->mark($this->className)->
add($listKey, $array, Cache::EXPIRES_FOREVER);
if ($array !== Cache::NOT_FOUND)
foreach ($array as $object)
$this->cacheById($object);
$pool->free($semKey);
}
return $array;
}
//@}
/// uncachers
//@{
public function uncacheLists()
{
$intKey = $this->keyToInt($this->indexKey);
return $this->registerUncacher(
UncacherSmartDaoWorkerLists::create($this->className, $this->indexKey, $intKey)
);
}
//@}
/// internal helpers
//@{
protected function gentlyGetByKey($key)
{
if ($object = Cache::me()->mark($this->className)->get($key)) {
if ($this->checkMap($key)) {
return $object;
} else {
Cache::me()->mark($this->className)->delete($key);
}
}
return null;
}
private function syncMap($objectKey)
{
$cache = Cache::me();
$mapExists = true;
if (!$map = $cache->mark($this->className)->get($this->indexKey)) {
$map = array();
$mapExists = false;
}
$map[$objectKey] = true;
if ($mapExists) {
$cache->mark($this->className)->
replace($this->indexKey, $map, Cache::EXPIRES_FOREVER);
} else {
$cache->mark($this->className)->
set($this->indexKey, $map, Cache::EXPIRES_FOREVER);
}
return true;
}
private function checkMap($objectKey)
{
$pool = SemaphorePool::me();
$semKey = $this->keyToInt($this->indexKey);
if (!$pool->get($semKey))
return false;
if (!$map = Cache::me()->mark($this->className)->get($this->indexKey)) {
$pool->free($semKey);
return false;
}
if (!isset($map[$objectKey])) {
$pool->free($semKey);
return false;
}
$pool->free($semKey);
return true;
}
//@}
}
?>