Cache.class.php 1.12 KB
<?php
class Util_Cache {
	public static $drivers = array ('Memcache', 'Memcached', 'Redis' );
	private static $MyDriver = 'memcached';
	/**
	 * 生成缓存对象
	 *
	 * @param string $driver
	 * @param array $options
	 * @return Q_Cache_Adapter_Interface
	 */
	public static function factory(array $options) {
	    if(empty($options['driver'])) {
	        $driver = self::$MyDriver;
	    } else {
	       $driver = $options['driver'];
	    }
		if (! in_array ( ucfirst ( $driver ), self::$drivers ))
			throw new Util_Cache_Exception ( '您选择的驱动系统不支持(' . ucfirst ( $driver ) . ')!' );
		if(ucfirst($driver) == 'Redis') {
		   return  new Util_Cache_Adapter_Redis($options);
		} else {
    		if (! isset ( $options ) || ! (isset ( $options ['domain'] ))) {
                throw new Util_Cache_Exception ( '请输入您需要的domain!' );
            }
            else {
                $classname = 'Util_Cache_Adapter_' . ucfirst ( $driver );
                return isset ( $options ['class'] ) ?  new $classname ( $options ['domain'], $options ['class'] ) : new $classname ( $options ['domain'] );
            }
		}
		
	}
}