Cache.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. class Core_Cache {
  3. var $cache;
  4. public function __construct() {
  5. $this->cache = array();
  6. }
  7. public static function &getInstance() {
  8. static $_instance;
  9. if (!is_object($_instance)) {
  10. $_instance = new Core_Cache();
  11. }
  12. return $_instance;
  13. }
  14. public static function get($key, $default = null) {
  15. return self::getInstance()->_get($key, $default);
  16. }
  17. public static function get_and_clean($key, $default = null) {
  18. return self::getInstance()->_get_and_clean($key, $default);
  19. }
  20. public static function set($key, $value) {
  21. return self::getInstance()->_set($key, $value);
  22. }
  23. public function &_get($key, $default = null) {
  24. $null = null;
  25. if ($key === null) return $null;
  26. if (array_key_exists($key, $this->cache)) {
  27. return $this->cache[$key];
  28. } else {
  29. return $default;
  30. }
  31. }
  32. public function &_get_and_clean($key, $default = null) {
  33. if (array_key_exists($key, $this->cache)) {
  34. $ret = $this->cache[$key];
  35. unset($this->cache[$key]);
  36. return $ret;
  37. } else {
  38. return $default;
  39. }
  40. }
  41. public function _set($key, $value) {
  42. $this->cache[$key] = $value;
  43. return true;
  44. }
  45. public function dbg() {
  46. return self::_('dbg','all','values');
  47. }
  48. }