Advertisement
retesere20

php-memory-ram-shared-caching

Feb 8th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1.  
  2.  
  3.  
  4. // ########## Caching ########## //
  5. /*
  6. public function save_cach($data, $name, $timeout) {
  7. return $this->save_cache($data, $name, $timeout);
  8. }
  9. public function save_cache($data, $name, $timeout) {
  10. // delete cache
  11. $id=shmop_open($this->get_cache_id($name), "a", 0, 0);
  12. shmop_delete($id);
  13. shmop_close($id);
  14.  
  15. // get id for name of cache
  16. $id=shmop_open($this->get_cache_id($name), "c", 0644, strlen(serialize($data)));
  17.  
  18. // return int for data size or boolean false for fail
  19. if ($id) {
  20. $this->set_timeout($name, $timeout);
  21. return shmop_write($id, serialize($data), 0);
  22. }
  23. else return false;
  24. }
  25. public function get_cache($name) {
  26. if (!check_timeout($name)) {
  27. $id=shmop_open($this->get_cache_id($name), "a", 0, 0);
  28.  
  29. if ($id) $data=unserialize(shmop_read($id, 0, shmop_size($id)));
  30. else return false; // failed to load data
  31.  
  32. if ($data) { // array retrieved
  33. shmop_close();
  34. return $data;
  35. }
  36. else return false; // failed to load data
  37. }
  38. else return false; // data was expired
  39. }
  40. public function get_cache_id($name) {
  41. // maintain list of caches here
  42. $id=array('test1' => 1, 'test2' => 2 );
  43. return $id[$name];
  44. }
  45. //
  46. public function set_timeout($name, $int) {
  47. $timeout=new DateTime(date('Y-m-d H:i:s'));
  48. date_add($timeout, date_interval_create_from_date_string("$int seconds"));
  49. $timeout=date_format($timeout, 'YmdHis');
  50.  
  51. $id=shmop_open(100, "a", 0, 0);
  52. if ($id) $tl=unserialize(shmop_read($id, 0, shmop_size($id)));
  53. else $tl=array();
  54. shmop_delete($id);
  55. shmop_close($id);
  56.  
  57. $tl[$name]=$timeout;
  58. $id=shmop_open(100, "c", 0644, strlen(serialize($tl)));
  59. shmop_write($id, serialize($tl), 0);
  60. }
  61. public function check_timeout($name) {
  62. $now=new DateTime(date('Y-m-d H:i:s'));
  63. $now=date_format($now, 'YmdHis');
  64.  
  65. $id=shmop_open(100, "a", 0, 0);
  66. if ($id) $tl=unserialize(shmop_read($id, 0, shmop_size($id)));
  67. else return true;
  68. shmop_close($id);
  69.  
  70. $timeout=$tl[$name];
  71. return (intval($now)>intval($timeout));
  72. }
  73. */
  74. // ############################### //
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement