summaryrefslogtreecommitdiff
path: root/framework/DataAccess/SQLMap/Configuration/TSqlMapCacheModel.php
blob: 3bd209334f7a322c8aee75b0b5e42b8be1206bb7 (plain)
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
<?php

class TSqlMapCacheModel extends TComponent
{
	private $_cache;
//	private $_flushInterval = -1;
	private $_hits = 0;
	private $_requests = 0;
	private $_id;
//	private $_lastFlush;
	private $_implementation;
	private $_properties = array();


	public function getID(){ return $this->_id; }
	public function setID($value){ $this->_id = $value; }

	public function getImplementation(){ return $this->_implementation; }
	public function setImplementation($value){ $this->_implementation = $value; }

//	public function getFlushInterval(){ return $this->_flushInterval; }
//	public function setFlushInterval($value){ $this->_flushInterval = $value; }

	public function initialize($sqlMap)
	{
		$implementation = $this->getImplementationClass(
				$sqlMap->getTypeHandlerFactory());
		$this->_cache = new $implementation;
		$this->_cache->configure($this, $this->_properties);
	}

	protected function getImplementationClass($typeFactory)
	{
		switch(strtolower($this->_implementation))
		{
			case 'fifo': return 'TSqlMapFifoCache';
			case 'lru' : return 'TSqlMapLruCache';
			case 'basic' : return 'TSqlMapApplicationCache';
		}

		if(class_exists($this->_implementation, false))
			$class = $this->_implementation;
		else
			$class  = $typeFactory->getTypeHandler($this->_implementation);
		if(!is_null($class)) 
			return $class;
		else
			throw new TSqlMapConfigurationException(
				'sqlmap_unable_to_find_implemenation', $this->_implementation);
	}

	public function addProperty($name, $value)
	{
		$this->_properties[strtolower($name)] = $value;
	}

	public function registerTriggerStatement($mappedStatement)
	{
		$mappedStatement->attachEventHandler('OnExecuteQuery', 
				array($this, 'flushHandler'));
	}

	protected function flushHandler($sender, $param)
	{
		$this->flush();
	}

	public function flush()
	{
		$this->_cache->flush();
	}

	public function get($key)
	{
		if($key instanceof TSqlMapCacheKey)
			$key = $key->getHash();

		//if flush ?
		$value = $this->_cache->get($key);
		$this->_requests++;
		if(!is_null($value))
			$this->_hits++;
		return $value;
	}

	public function set($key, $value)
	{
		if($key instanceof TSqlMapCacheKey)
			$key = $key->getHash();

		if(!is_null($value))
			$this->_cache->set($key, $value);
	}

	public function getHitRatio()
	{
		if($this->_requests != 0)
			return $this->_hits / $this->_requests;
		else
			return 0;
	}
}


class TSqlMapCacheKey
{
	private $_key;

	public function __construct($object)
	{
		$this->_key = $this->generateKey(serialize($object));
	}

	protected function generateKey($string)
	{
		return sprintf('%x',crc32($string));
	}

	public function getHash()
	{
		return $this->_key;
	}

	public function __toString()
	{
		return $this->getHash();
	}
}


?>