Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Moja implementacja "keszu"
q.michal
post
Post #1





Grupa: Zarejestrowani
Postów: 111
Pomógł: 1
Dołączył: 24.12.2013

Ostrzeżenie: (0%)
-----


Witam,

Chcialbym dzis pdozielic sie swoim kolejnym wynalazkiem - obsluga keszu. Jest to co prawda dopiero zalazek tego co chce zrobic, albowiem brakuje metod odpowiedzialnych za obsluge danych, niemniej sposob 'komunikacji' ze sterownikiem widac na przykladzie metody Cache::dumpCache(); Docelowo zamierzam napisac takze wiecej sterownikow, zapewniajac dostep do: APC, eAccelerator, Dummy, File, MemCache, SQLite, WinCache, XCache. Zastanawiam sie takze nad mozliwoscia przechowywania keszu w cookies.

Jednoczesnie prosze o ocene oraz komentarze - czyli ogolnie mowiac code review (IMG:style_emoticons/default/wink.gif)
Jestem otwarty na wszelkie sugestie i uwagi poparte jakimkolwiek argumentem.

Pozdrawiam serdecznie.


  1. abstract class CacheAbstract {
  2.  
  3. /**
  4. * Disables object cloning
  5. *
  6. * @return void does not return any value
  7. */
  8. protected function __clone() {
  9. throw new Exception('Cloning Cache objects is not allowed!');
  10. }
  11.  
  12. /**
  13. * Class constructor
  14. *
  15. * @return void does not return any value
  16. */
  17. protected function __construct() {
  18. }
  19.  
  20. } /* class */
  21.  
  22.  
  23. interface CacheDriver {
  24.  
  25. /**
  26. * Adds an object to the cache
  27. *
  28. * @param string stores the variable using this name, should be a cache-unique name
  29. * @param mixed the variable to store in cache
  30. * @param integer time-to-live of cached data
  31. * @return boolean TRUE on success, FALSE otherwise
  32. */
  33. public function addValue($key, $var, $ttl = 300);
  34.  
  35. /**
  36. * Returns a dump of all files and user variables from the cache
  37. *
  38. * @return string dump of the user variables, or NULL if an unknown error encountered
  39. */
  40. public function dumpCache();
  41.  
  42. /**
  43. * Flushes (clears) the user and system cache
  44. *
  45. * @return void does not return any value
  46. */
  47. public function flushCache();
  48.  
  49. /**
  50. * Provides a statistics and meta-data of caching subsystem
  51. *
  52. * @return array array containing cache statistics
  53. */
  54. public function getStatistics();
  55.  
  56. /**
  57. * Fetches a stored variable from the cache
  58. *
  59. * @param string key name used to store the value
  60. * @return mixed stored variable on success, FALSE otherwise
  61. */
  62. public function getValue($key);
  63.  
  64. /**
  65. * Checks if container contains an item stored under this key name
  66. *
  67. * @param string key name used to store the value
  68. * @return boolean TRUE if the key exists, FALSE otherwise
  69. */
  70. public function isCached($key);
  71.  
  72. /**
  73. * Checks whether the chosen driver is supported by PHP installation
  74. *
  75. * @return boolean TRUE if driver can be used, FALSE otherwise
  76. */
  77. public function isSupported();
  78.  
  79. /**
  80. * Removes a stored variable from the cache
  81. * @param string key name used to store the value
  82. * @return boolean TRUE on success, FALSE otherwise
  83. */
  84. public function removeValue($key);
  85.  
  86. } /* class */
  87.  
  88.  
  89. final class CacheDriverApc extends CacheAbstract implements CacheDriver {
  90.  
  91. /**
  92. * Adds an object to the cache
  93. *
  94. * @param string stores the variable using this name, should be a cache-unique name
  95. * @param mixed the variable to store in cache
  96. * @param integer time-to-live of cached data
  97. * @return boolean TRUE on success, FALSE otherwise
  98. */
  99. public function addValue($key, $var, $ttl = 300) {
  100. return apc_store($key, $var, $ttl);
  101. }
  102.  
  103. /**
  104. * Returns a dump of all files and user variables from the cache
  105. *
  106. * @return string dump of the user variables, or NULL if an unknown error encountered
  107. */
  108. public function dumpCache() {
  109. return apc_bin_dump();
  110. }
  111.  
  112. /**
  113. * Flushes (clears) the user and system cache
  114. *
  115. * @return void does not return any value
  116. */
  117. public function flushCache() {
  118. apc_clear_cache();
  119. apc_clear_cache('user');
  120. }
  121.  
  122. /**
  123. * Provides a statistics and meta-data of caching subsystem
  124. *
  125. * @return array array containing cache statistics
  126. */
  127. public function getStatistics() {
  128. return array('data' => apc_cache_info('user'), 'info' => '', 'size' => '');
  129. }
  130.  
  131. /**
  132. * Fetches a stored variable from the cache
  133. *
  134. * @param string key name used to store the value
  135. * @return mixed stored variable on success, FALSE otherwise
  136. */
  137. public function getValue($key) {
  138. $var = apc_fetch($key, $result);
  139. if(!$result) {
  140. return false;
  141. }
  142. return $var;
  143. }
  144.  
  145. /**
  146. * Checks if container contains an item stored under this key name
  147. *
  148. * @param string key name used to store the value
  149. * @return boolean TRUE if the key exists, FALSE otherwise
  150. */
  151. public function isCached($key) {
  152. return (bool) apc_exists($key);
  153. }
  154.  
  155. /**
  156. * Checks whether the chosen driver is supported by PHP installation
  157. *
  158. * @return boolean TRUE if driver can be used, FALSE otherwise
  159. */
  160. public function isSupported() {
  161. if(!extension_loaded('apc') || !ini_get('apc.enabled')) {
  162. return false;
  163. }
  164. return true;
  165. }
  166.  
  167. /**
  168. * Removes a stored variable from the cache
  169. * @param string key name used to store the value
  170. * @return boolean TRUE on success, FALSE otherwise
  171. */
  172. public function removeValue($key) {
  173. return apc_delete($key);
  174. }
  175.  
  176. } /* class */
  177.  
  178.  
  179. final class Cache extends CacheAbstract {
  180. private $driver;
  181. private $storage;
  182.  
  183. /**
  184. * Class constructor.
  185. *
  186. * @param string specifies primary cache driver
  187. * @param string specifies secondary (failover) cache driver
  188. * @return object the Cache object
  189. */
  190. protected function __construct($driver, $failover) {
  191. if(!$this->loadDriver($driver)) {
  192. if(!$this->loadDriver($failover)) {
  193. $this->loadDriver('dummy');
  194. }
  195. }
  196. }
  197.  
  198. /**
  199. * Returns a dump of all files and user variables from the cache
  200. *
  201. * @return string dump of the user variables, or NULL if an unknown error encountered
  202. */
  203. public function dumpCache() {
  204. return $this->storage->dumpCache();
  205. }
  206.  
  207. /**
  208. * Returns a name of currently used cache storage driver
  209. *
  210. * @return string name of used storage driver
  211. */
  212. public function getDriver() {
  213. return $this->driver;
  214. }
  215.  
  216. /**
  217. * Returns the global Cache object, creating it only if it does not exist already
  218. *
  219. * @param string specifies primary cache driver
  220. * @param string specifies secondary (failover) cache driver
  221. * @return object the Cache object
  222. */
  223. public static function getInstance($driver = 'file', $failover = 'dummy') {
  224. static $instance;
  225. if(!isset($instance)) {
  226. $instance = new Cache($driver, $failover);
  227. }
  228. return $instance;
  229. }
  230.  
  231. /**
  232. * Returns a list of available cache storage drivers
  233. *
  234. * @return array an array containing a list of available cache drivers
  235. */
  236. public static function getStorageList() {
  237. $list = array();
  238. $directory = System::getSystemDir() . '/cache/driver';
  239. $dh = opendir($directory);
  240. while(($filename = readdir($dh)) !== false) {
  241. $driver = substr($filename, 0, strrpos($filename, '.'));
  242. if($driver != '' && $driver != '.') {
  243. $list[] = $driver;
  244. }
  245. }
  246. return $list;
  247. }
  248.  
  249. /**
  250. * Loads the specified cache driver
  251. *
  252. * @param string cache driver to load
  253. * @return boolean TRUE on success, FALSE otherwise
  254. */
  255. private function loadDriver($driver) {
  256. if(in_array($driver, $this->getStorageList())) {
  257. $class = 'CacheDriver' . ucfirst($driver);
  258. $this->storage = new $class();
  259. if($this->storage->isSupported()) {
  260. $this->driver = $driver;
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266.  
  267. } /* class */

Go to the top of the page
+Quote Post

Posty w temacie
- q.michal   Moja implementacja "keszu"   23.02.2016, 10:13:40
- - Pyton_000   Robisz to dla treningu czy do użytku? Jest tyle g...   23.02.2016, 11:14:06
|- - q.michal   Oba powody sa poprawne. Przede wszystkim chce sie ...   23.02.2016, 11:19:34
- - daro0   Cache w cookies? To ma jakiekolwiek praktyczne zas...   27.02.2016, 08:12:05
- - q.michal   Jak to mowia, takie hobby ;-) Przyznam szczerze z...   27.02.2016, 12:32:15
- - daro0   Też na początku się zastanawiałem po co te podkata...   27.02.2016, 12:48:23
- - q.michal   Brzmi sensownie, ale tez we wszystko bym bezgranic...   27.02.2016, 12:57:35
- - daro0   Co do serializacji to też chyba nie jest takie pro...   27.02.2016, 13:11:00
- - q.michal   Ciekawe zestawienie. Watpie jednak, aby ktos pakow...   27.02.2016, 13:21:49
- - Pyton_000   Zawsze możesz stworzyć "tablicę" file ca...   27.02.2016, 14:53:23
|- - q.michal   Cytat(Pyton_000 @ 27.02.2016, 14:53:2...   27.02.2016, 14:59:36
- - Pyton_000   Musisz ją zapisać. Wydało mi się to logiczne.   27.02.2016, 15:20:44
- - daro0   O jakich tablicach Wy w ogóle piszecie? Nic takieg...   27.02.2016, 17:19:17
- - Pyton_000   @daro0 To oświeć mnie jak zapiszesz obiekt do Cach...   27.02.2016, 19:10:06
- - daro0   Tak w dużym uproszczeniu: [PHP] pobierz, plaintex...   27.02.2016, 19:48:46
- - Pyton_000   Ja wiem że trzeba obiekt zserializować. Ale... C...   27.02.2016, 19:53:35
- - q.michal   Odnoszac sie do benchmarku zalaczonego przez daro0...   27.02.2016, 21:32:28
- - daro0   To rand(0,99) jest chyba niezbyt dokładne. Można j...   28.02.2016, 07:37:29
- - q.michal   Raczej malo trafiony przyklad Dane pobierzesz, pr...   29.02.2016, 16:13:24


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 17.09.2025 - 10:43