fork download
  1. <?php
  2.  
  3. class MyCache {
  4. static $instance = null;
  5. static $ary = array();
  6. protected $status = 0;
  7.  
  8. private function __construct(){}
  9.  
  10. static function getInstance() {
  11. if (is_null(self::$instance)) {
  12. self::$instance = new self();
  13. }
  14. return self::$instance;
  15. }
  16.  
  17. function get($key) {
  18. if (isset(self::$ary[$key]) && self::$ary[$key]['time'] > microtime(true)) {
  19. $this->status = 0;
  20. return self::$ary[$key]['value'];
  21. } else {
  22. $this->status = 1;
  23. return false;
  24. }
  25. }
  26.  
  27. function set($key, $value, $time) {
  28. self::$ary[$key] = array(
  29. 'value' => $value,
  30. 'time' => $time + microtime(true),
  31. );
  32. }
  33.  
  34. function getOrUpdate($key, $time) {
  35. $value = $this->get($key);
  36. if (($value === false) && ($this->status != 0)) {
  37. $bt = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1];
  38. $params = $bt['args'];
  39. $params[count($params)-1] = false;
  40. isset($bt['type'])
  41. ? array(
  42. ($bt['type'] == '->') ? $bt['object'] : $bt['class'],
  43. $bt['function']
  44. )
  45. : $bt['function'],
  46. $params
  47. );
  48. $this->set($key, $value, $time);
  49. }
  50. return $value;
  51. }
  52. }
  53.  
  54.  
  55. function globalLol($cache = false) {
  56. if ($cache) {
  57. return MyCache::getInstance()->getOrUpdate('lol', 0.5);
  58. }
  59. echo "# new lol\n";
  60. return microtime();
  61. }
  62.  
  63. class Pysch {
  64. function instanceLol($cache = false) {
  65. if ($cache) {
  66. return MyCache::getInstance()->getOrUpdate('instance_lol', 0.5);
  67. }
  68. echo "# new instance lol\n";
  69. return microtime();
  70. }
  71. static function staticLol($cache = false) {
  72. if ($cache) {
  73. return MyCache::getInstance()->getOrUpdate('static_lol', 0.5);
  74. }
  75. echo "# new static lol\n";
  76. return microtime();
  77. }
  78. }
  79.  
  80. var_dump(globalLol(true));
  81. var_dump(globalLol(true));
  82. sleep(1);
  83. var_dump(globalLol(true));
  84. var_dump(globalLol(true));
  85.  
  86. var_dump(Pysch::staticLol(true));
  87. var_dump(Pysch::staticLol(true));
  88. sleep(1);
  89. var_dump(Pysch::staticLol(true));
  90. var_dump(Pysch::staticLol(true));
  91.  
  92. $pysch = new Pysch();
  93. var_dump($pysch->instanceLol(true));
  94. var_dump($pysch->instanceLol(true));
  95. sleep(1);
  96. var_dump($pysch->instanceLol(true));
  97. var_dump($pysch->instanceLol(true));
  98.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
# new lol
string(21) "0.90812700 1361103124"
string(21) "0.90812700 1361103124"
# new lol
string(21) "0.90837500 1361103125"
string(21) "0.90837500 1361103125"
# new static lol
string(21) "0.90850200 1361103125"
string(21) "0.90850200 1361103125"
# new static lol
string(21) "0.90872900 1361103126"
string(21) "0.90872900 1361103126"
# new instance lol
string(21) "0.90884200 1361103126"
string(21) "0.90884200 1361103126"
# new instance lol
string(21) "0.90907400 1361103127"
string(21) "0.90907400 1361103127"