Cache.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2005-2008 by Anton E. Lebedevich, Konstantin V. Arkhipov *
00004  *                                                                          *
00005  *   This program is free software; you can redistribute it and/or modify   *
00006  *   it under the terms of the GNU Lesser General Public License as         *
00007  *   published by the Free Software Foundation; either version 3 of the     *
00008  *   License, or (at your option) any later version.                        *
00009  *                                                                          *
00010  ****************************************************************************/
00011     
00022     final class Cache extends StaticFactory implements Instantiatable
00023     {
00024         const NOT_FOUND         = 'nil';
00025         
00026         const EXPIRES_FOREVER   = 604800; // 7 days
00027         const EXPIRES_MAXIMUM   = 21600; // 6 hrs
00028         const EXPIRES_MEDIUM    = 3600; // 1 hr
00029         const EXPIRES_MINIMUM   = 300; // 5 mins
00030         
00031         const DO_NOT_CACHE      = -2005;
00032         
00034         private static $map     = null;
00035         
00037         private static $peer    = null;
00038         
00040         private static $worker  = null;
00041         
00043         private static $instances = array();
00044         
00048         public static function me()
00049         {
00050             if (!self::$peer || !self::$peer->isAlive())
00051                 self::$peer = new RuntimeMemory();
00052             
00053             return self::$peer;
00054         }
00055         
00056         /* void */ public static function setPeer(CachePeer $peer)
00057         {
00058             self::$peer = $peer;
00059         }
00060         
00061         /* void */ public static function setDefaultWorker($worker)
00062         {
00063             Assert::classExists($worker);
00064             
00065             self::$worker = $worker;
00066         }
00067         
00071         public static function setDaoMap($map)
00072         {
00073             self::$map = $map;
00074         }
00075         
00076         public static function appendDaoMap($map)
00077         {
00078             if (self::$map)
00079                 self::$map = array_merge(self::$map, $map);
00080             else
00081                 self::setDaoMap($map);
00082         }
00083         
00087         public static function worker($dao)
00088         {
00089             $class = get_class($dao);
00090             
00091             if (!isset(self::$instances[$class])) {
00092                 if (isset(self::$map[$class])) {
00093                     $className = self::$map[$class];
00094                     self::$instances[$class] = new $className($dao);
00095                 } elseif ($worker = self::$worker)
00096                     self::$instances[$class] = new $worker($dao);
00097                 else
00098                     self::$instances[$class] = new CommonDaoWorker($dao);
00099             }
00100             
00101             return self::$instances[$class];
00102         }
00103         
00104         /* void */ public static function dropWorkers()
00105         {
00106             self::$instances = array();
00107         }
00108     }
00109 ?>