Guest User

KrakenApiCache.php

a guest
Feb 20th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2.  
  3. class KrakenApiCache{
  4.  
  5.     private static $cache = array();    
  6.  
  7.    
  8.     /**
  9.     * Cache a variabele for X time
  10.     *
  11.     * @param string $key
  12.     * @param mixed $value
  13.     * @param float $timeout seconds (0.1, 0.5, 1, 10)
  14.     */
  15.     public static function set($key, $value, $timeout = 1){
  16.        
  17.         self::$cache[$key] = array(
  18.             'value'     => $value,
  19.             'timeout'   => $timeout,
  20.             'microtime' => microtime(true)
  21.         );
  22.     }
  23.    
  24.     /**
  25.     * Get a variable from cache
  26.     * Or return false if ttl is passed
  27.     *
  28.     * @param string $key
  29.     */
  30.     public static function get($key){
  31.        
  32.         if (isset(self::$cache[$key])){
  33.             $timeout = self::$cache[$key]['timeout'];
  34.            
  35.             if (abs(microtime(true) - self::$cache[$key]['microtime']) <= self::$cache[$key]['timeout']){
  36.                 return self::$cache[$key]['value'];      
  37.             }
  38.         }
  39.         return false;
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment