Cache.php 1.1 KB

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