summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Data/SqlMap/DataMapper/TFastSqlMapApplicationCache.php
blob: ec059781bcf2137ddf5a238f71ed752b6a139015 (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
<?php
/**
 * TFastSqlMapApplicationCache class file contains Fast SqlMap cache implementation.
 *
 * @author Berczi Gabor <gabor.berczi@devworx.hu>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2015 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Data.SqlMap
 */

/**
 * TFastSqlMapApplicationCache class file
 *
 * Fast SqlMap result cache class with minimal-concurrency get/set and atomic flush operations
 *
 * @author Berczi Gabor <gabor.berczi@devworx.hu>
 * @package System.Data.SqlMap
 * @since 3.2
 */

class TFastSqlMapApplicationCache implements ICache
{
	protected $_cacheModel=null;
	protected $_cache=null;

	public function __construct($cacheModel=null)
	{
		$this->_cacheModel = $cacheModel;
	}

	protected function getBaseKeyKeyName()
	{
		return 'SqlMapCacheBaseKey::'.$this->_cacheModel->getId();
	}

	protected function getBaseKey()
	{
		$cache = $this->getCache();
		$keyname = $this->getBaseKeyKeyName();
		$basekey = $cache->get($keyname);
		if (!$basekey)
		{
			$basekey = DxUtil::generateRandomHash(8);
			$cache->set($keyname,$basekey);
		}
		return $basekey;
	}

	protected function getCacheKey($key)
	{
		return $this->getBaseKey().'###'.$key;
	}

	public function delete($key)
	{
		$this->getCache()->delete($this->getCacheKey($key));
	}

	public function flush()
	{
		$this->getCache()->delete($this->getBaseKeyKeyName());
	}

	public function get($key)
	{
		$result = $this->getCache()->get($this->getCacheKey($key));
		return $result === false ? null : $result;
	}

	public function set($key, $value,$expire=0,$dependency=null)
	{
		$this->getCache()->set($this->getCacheKey($key), $value, $expire,$dependency);
	}

	protected function getCache()
	{
		if (!$this->_cache)
			$this->_cache = Prado::getApplication()->getCache();
		return $this->_cache;
	}

	public function add($id,$value,$expire=0,$dependency=null)
	{
		throw new TSqlMapException('sqlmap_use_set_to_store_cache');
	}
}