-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReaderCache.php
140 lines (112 loc) · 4.52 KB
/
ReaderCache.php
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
<?php
namespace sporm;
class ReaderCache {
private $aSingleObjectCache;
private $aFilterCache;
private $aAlternativeIdsSingleObjectCache;
/**
* @var OrmRegister
*/
private $oOrmRegister;
/**
* @var ReaderCache
*/
static private $oInstance;
private function __construct( OrmRegister $oOrmRegister ) {
$this->clearCache();
$this->oOrmRegister = $oOrmRegister;
}
static function getInstance( OrmRegister $oOrmRegister ) {
if ( !isset( self::$oInstance ) ) {
self::$oInstance = new ReaderCache( $oOrmRegister );
}
return self::$oInstance;
}
function clearCache() {
$this->aSingleObjectCache = array();
$this->aFilterCache = array();
$this->aAlternativeIdsSingleObjectCache = array();
}
// TODO: Group the filter, order by and limit into a query object
function getDataFromCache( $sObjectType, Filter $oFilter, $oOrderBy, $oLimit ) {
$sId = $this->buildHash( $oFilter, $oOrderBy, $oLimit );
if ( isset( $this->aFilterCache[ $sObjectType ][ $sId ] ) ) {
return $this->aFilterCache[ $sObjectType ][ $sId ];
}
$aFields = $this->getEqualsFields( $oFilter );
if ( $aFields ) {
$oCompositeId = new CompositeId( $aFields );
// echo( "Looking in cache based on filter: $oCompositeId <br/>");
$oMapping = $this->oOrmRegister->getOrmConfiguration( $sObjectType );
if ( $oMapping->isAnAlternativeId( $oCompositeId ) ) {
// echo( 'Might be in the cache!<br/>');
if ( $this->singleObjectIsInAlternativeIdCache( $sObjectType, $oCompositeId ) ) {
// echo( 'Get it from the cache<br/>');
return array( $this->getSingleObjectFromAlternativeIdCache( $sObjectType, $oCompositeId ) );
}
// echo( 'Not in the cache<br/>');
}
}
return false;
}
function getEqualsFields( Filter $oFilter, $aFilterFields = array() ) {
if ( $oFilter->getComparitor() == '=' ) {
$aFilterFields[ $oFilter->getField() ] = $oFilter->getValue();
if ( $oFilter->hasAnd() ) {
$aFilterFields = $this->getEqualsFields( $oFilter->getAnd(), $aFilterFields );
}
return $aFilterFields;
}
return false;
}
function putObjectsIntoCache( $sObjectType, Filter $oFilter, $oOrderBy, $oLimit, $aObjects ) {
foreach( $aObjects as $oThisObject ) {
$this->putSingleObjectIntoCache( $sObjectType, $oThisObject );
$this->putSingleObjectIntoAlternativeIdCache( $sObjectType, $oThisObject );
}
$this->putCollectionIntoCache( $sObjectType, $oFilter, $oOrderBy, $oLimit, $aObjects );
}
function putSingleObjectIntoCache( $sObjectType, $oObject ) {
$sId = $oObject->getId();
$this->aSingleObjectCache[ $sObjectType ][ $sId ] = $oObject;
}
function putSingleObjectIntoAlternativeIdCache( $sObjectType, $oObject ) {
$oMapping = $this->oOrmRegister->getOrmConfiguration( $sObjectType );
$aAlternativeIds = $oMapping->getAlternativeIds();
foreach( $aAlternativeIds as $oIdDefinition ) {
$sId = $oIdDefinition->extractIdAsStringFromObject( $oObject );
// echo( "Adding to alternative id cache: $sObjectType, $oIdDefinition = $sId<br/>");
$this->aAlternativeIdsSingleObjectCache[ $sObjectType ][ (String)$oIdDefinition ][ $sId ] = $oObject;
}
}
function singleObjectIsInAlternativeIdCache( $sObjectType, CompositeId $oIdDefinition ) {
$sId = $oIdDefinition->extractIdValuesAsString( $oIdDefinition );
// echo( "Looking in cache for $sObjectType, $oIdDefinition = $sId<br/>");
return isset( $this->aAlternativeIdsSingleObjectCache[ $sObjectType ][ (String)$oIdDefinition ][ $sId ] );
}
function getSingleObjectFromAlternativeIdCache( $sObjectType, CompositeId $oIdDefinition ) {
$sId = $oIdDefinition->extractIdValuesAsString( $oIdDefinition );
return $this->aAlternativeIdsSingleObjectCache[ $sObjectType ][ (String)$oIdDefinition ][ $sId ];
}
function singleObjectIsInCache( $sObjectType, $sId ) {
$bExists = ( isset( $this->aSingleObjectCache[ $sObjectType ][ $sId ] ) );
return $bExists;
}
function getSingleObjectFromCache( $sObjectType, $sId ) {
return $this->aSingleObjectCache[ $sObjectType ][ $sId ];
}
private function putCollectionIntoCache( $sObjectType, Filter $oFilter, $oOrderBy, $oLimit, $aObjects ) {
$this->aFilterCache[ $sObjectType ][ $this->buildHash( $oFilter, $oOrderBy, $oLimit ) ] = $aObjects;
}
private function buildHash( $oFilter, $oOrderBy, $oLimit ) {
$sOrderByHash = '';
$sLimitHash = '';
if ( $oOrderBy ) {
$sOrderByHash = $oOrderBy->getHash();
}
if ( $oLimit ) {
$sLimitHash = $oLimit->getHash();
}
return $oFilter->getHash().';'.$sOrderByHash.';'.$sLimitHash;
}
}