1<?php
2
3/*
4 * This file is part of MailSo.
5 *
6 * (c) 2014 Usenko Timur
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace MailSo\Cache;
13
14/**
15 * @category MailSo
16 * @package Cache
17 */
18class CacheClient
19{
20	/**
21	 * @var \MailSo\Cache\DriverInterface
22	 */
23	private $oDriver;
24
25	/**
26	 * @var string
27	 */
28	private $sCacheIndex;
29
30	/**
31	 * @access private
32	 */
33	private function __construct()
34	{
35		$this->oDriver = null;
36		$this->sCacheIndex = '';
37	}
38
39	/**
40	 * @return \MailSo\Cache\CacheClient
41	 */
42	public static function NewInstance()
43	{
44		return new self();
45	}
46
47	/**
48	 * @param string $sKey
49	 * @param string $sValue
50	 *
51	 * @return bool
52	 */
53	public function Set($sKey, $sValue)
54	{
55		return $this->oDriver ? $this->oDriver->Set($sKey.$this->sCacheIndex, $sValue) : false;
56	}
57
58	/**
59	 * @param string $sKey
60	 *
61	 * @return bool
62	 */
63	public function SetTimer($sKey)
64	{
65		return $this->Set($sKey.'/TIMER', time());
66	}
67
68	/**
69	 * @param string $sKey
70	 *
71	 * @return bool
72	 */
73	public function SetLock($sKey)
74	{
75		return $this->Set($sKey.'/LOCK', '1');
76	}
77
78	/**
79	 * @param string $sKey
80	 *
81	 * @return bool
82	 */
83	public function RemoveLock($sKey)
84	{
85		return $this->Set($sKey.'/LOCK', '0');
86	}
87
88	/**
89	 * @param string $sKey
90	 *
91	 * @return bool
92	 */
93	public function GetLock($sKey)
94	{
95		return '1' === $this->Get($sKey.'/LOCK');
96	}
97
98	/**
99	 * @param string $sKey
100	 * @param string $bClearAfterGet = false
101	 *
102	 * @return string
103	 */
104	public function Get($sKey, $bClearAfterGet = false)
105	{
106		$sValue = '';
107
108		if ($this->oDriver)
109		{
110			$sValue = $this->oDriver->Get($sKey.$this->sCacheIndex);
111		}
112
113		if ($bClearAfterGet)
114		{
115			$this->Delete($sKey);
116		}
117
118		return $sValue;
119	}
120
121	/**
122	 * @param string $sKey
123	 *
124	 * @return int
125	 */
126	public function GetTimer($sKey)
127	{
128		$iTimer = 0;
129		$sValue = $this->Get($sKey.'/TIMER');
130		if (0 < strlen($sValue) && is_numeric($sValue))
131		{
132			$iTimer = (int) $sValue;
133		}
134
135		return $iTimer;
136	}
137
138	/**
139	 * @param string $sKey
140	 *
141	 * @return \MailSo\Cache\CacheClient
142	 */
143	public function Delete($sKey)
144	{
145		if ($this->oDriver)
146		{
147			$this->oDriver->Delete($sKey.$this->sCacheIndex);
148		}
149
150		return $this;
151	}
152
153	/**
154	 * @param \MailSo\Cache\DriverInterface $oDriver
155	 *
156	 * @return \MailSo\Cache\CacheClient
157	 */
158	public function SetDriver(\MailSo\Cache\DriverInterface $oDriver)
159	{
160		$this->oDriver = $oDriver;
161
162		return $this;
163	}
164
165	/**
166	 * @param int $iTimeToClearInHours = 24
167	 *
168	 * @return bool
169	 */
170	public function GC($iTimeToClearInHours = 24)
171	{
172		return $this->oDriver ? $this->oDriver->GC($iTimeToClearInHours) : false;
173	}
174
175	/**
176	 * @return bool
177	 */
178	public function IsInited()
179	{
180		return $this->oDriver instanceof \MailSo\Cache\DriverInterface;
181	}
182
183	/**
184	 * @param string $sCacheIndex
185	 *
186	 * @return \MailSo\Cache\CacheClient
187	 */
188	public function SetCacheIndex($sCacheIndex)
189	{
190		$this->sCacheIndex = 0 < \strlen($sCacheIndex) ? "\x0".$sCacheIndex : '';
191
192		return $this;
193	}
194
195	/**
196	 * @param bool $bCache = false
197	 *
198	 * @return bool
199	 */
200	public function Verify($bCache = false)
201	{
202		if ($this->oDriver)
203		{
204			$sCacheData = \gmdate('Y-m-d-H');
205			if ($bCache && $sCacheData === $this->Get('__verify_key__'))
206			{
207				return true;
208			}
209
210			return $this->Set('__verify_key__', $sCacheData);
211		}
212
213		return false;
214	}
215}
216