Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2. define('DS', DIRECTORY_SEPARATOR);
  3. class Caches
  4. {
  5.     function save($data, $namespace, $id)
  6.     {
  7.         $path = $this->get_namespace_path($namespace) . $this->get_id_dir($id);
  8.         if (!file_exists($path))
  9.         {
  10.             mkdir($path, 0775, true);
  11.         }
  12.         file_put_contents($path . md5($id), $data);
  13.     }
  14.    
  15.     function load($namespace, $id, $lifetime = 0)
  16.     {
  17.         $filename = $this->get_namespace_path($namespace) . $this->get_id_path($id);
  18.         if (!file_exists($filename))
  19.             return false;
  20.         if (($lifetime != 0) && ((@filemtime($filename) + $lifetime) < time()))
  21.         {
  22.             $this->remove($namespace, $id);
  23.             return false;
  24.         }
  25.         $data = file_get_contents($filename);
  26.         return $data;
  27.     }
  28.    
  29.     function remove($namespace, $id)
  30.     {
  31.         $filename = $this->get_namespace_path($namespace) . $this->get_id_path($id);
  32.         if (file_exists($filename))
  33.             unlink($filename);
  34.     }
  35.  
  36.     function clean($namespace)
  37.     {
  38.         $filename = $this->get_namespace_path($namespace);
  39.  
  40.         if (file_exists($filename))
  41.             unlink($filename);
  42.     }
  43.  
  44.     private function get_id_path($id)
  45.     {
  46.         return $this->get_id_dir($id) . md5($id);
  47.     }
  48.  
  49.     private function get_id_dir($id)
  50.     {
  51.         $hash = md5($id);
  52.         return $hash[0] . DS . $hash[1] . DS;
  53.     }
  54.  
  55.     private function get_namespace_path($namespace)
  56.     {
  57.         return ZENGINE_HOMEDIR . DS . 'zcontent' . DS . 'cache' . DS . $namespace . DS;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement