模块

Cache_Sqlite
继承自 Cache

BootPHP 缓存 SQLite 驱动。

需要 SQLite3 与 PDO

package
BootPHP/缓存
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 MODPATH/cache/classes/cache/sqlite.php 第 14 行声明。

常量

DEFAULT_EXPIRE

integer 3600

属性

public static string $default

default driver to use

string(4) "file"

public static BootPHP_Cache $instances

instances

array(0) 

protected Config $_config

protected PDO $_db

Database resource

方法

public delete( string $id ) (在 Cache_Sqlite 中定义)

Delete a cache entry based on id

参数

  • string $id required - Id

Tags

返回值

  • boolean

源代码

public function delete($id)
{
	// Prepare statement
	$statement = $this->_db->prepare('DELETE FROM caches WHERE id = :id');
	// Remove the entry
	try
	{
		$statement->execute(array(':id' => $this->_sanitize_id($id)));
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	return (bool) $statement->rowCount();
}

public deleteAll( ) (在 Cache_Sqlite 中定义)

Delete all cache entries

返回值

  • boolean

源代码

public function deleteAll()
{
	// Prepare statement
	$statement = $this->_db->prepare('DELETE FROM caches');
	// Remove the entry
	try
	{
		$statement->execute();
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	return (bool) $statement->rowCount();
}

public delete_tag( string $tag ) (在 Cache_Sqlite 中定义)

Delete cache entries based on a tag

参数

  • string $tag required - Tag

Tags

返回值

  • boolean

源代码

public function delete_tag($tag)
{
	// Prepare the statement
	$statement = $this->_db->prepare('DELETE FROM caches WHERE tags LIKE :tag');
	// Try to delete
	try
	{
		$statement->execute(array(':tag' => "%<{$tag}>%"));
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	return (bool) $statement->rowCount();
}

public find( string $tag ) (在 Cache_Sqlite 中定义)

Find cache entries based on a tag

参数

  • string $tag required - Tag

Tags

返回值

  • array

源代码

public function find($tag)
{
	// Prepare the statement
	$statement = $this->_db->prepare('SELECT id, cache FROM caches WHERE tags LIKE :tag');
	// Try to find
	try
	{
		if (!$statement->execute(array(':tag' => "%<{$tag}>%")))
		{
			return array();
		}
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	$result = array();
	while ($row = $statement->fetchObject())
	{
		// Disable notices for unserializing
		$ER = error_reporting(~E_NOTICE);
		$result[$row->id] = unserialize($row->cache);
		// Turn notices back on
		error_reporting($ER);
	}
	return $result;
}

public garbage_collect( ) (在 Cache_Sqlite 中定义)

Garbage collection method that cleans any expired cache entries from the cache.

返回值

  • void

源代码

public function garbage_collect()
{
	// Create the sequel statement
	$statement = $this->_db->prepare('DELETE FROM caches WHERE expiration < :expiration');
	try
	{
		$statement->execute(array(':expiration' => time()));
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
}

public get( string $id [, string $default = NULL ] ) (在 Cache_Sqlite 中定义)

Retrieve a value based on an id

参数

  • string $id required - Id
  • string $default = NULL - Default [Optional] Default value to return if id not found

Tags

返回值

  • mixed

源代码

public function get($id, $default = NULL)
{
	// Prepare statement
	$statement = $this->_db->prepare('SELECT id, expiration, cache FROM caches WHERE id = :id LIMIT 0, 1');
	// Try and load the cache based on id
	try
	{
		$statement->execute(array(':id' => $this->_sanitize_id($id)));
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	if (!$result = $statement->fetch(PDO::FETCH_OBJ))
	{
		return $default;
	}
	// If the cache has expired
	if ($result->expiration != 0 and $result->expiration <= time())
	{
		// Delete it and return default value
		$this->delete($id);
		return $default;
	}
	// Otherwise return cached object
	else
	{
		// Disable notices for unserializing
		$ER = error_reporting(~E_NOTICE);

		// Return the valid cache data
		$data = unserialize($result->cache);
		// Turn notices back on
		error_reporting($ER);
		// Return the resulting data
		return $data;
	}
}

public set( string $id , mixed $data [, integer $lifetime = NULL ] ) (在 Cache_Sqlite 中定义)

Set a value based on an id. Optionally add tags.

参数

  • string $id required - Id
  • mixed $data required - Data
  • integer $lifetime = NULL - Lifetime [Optional]

返回值

  • boolean

源代码

public function set($id, $data, $lifetime = NULL)
{
	return (bool) $this->set_with_tags($id, $data, $lifetime);
}

public set_with_tags( string $id , mixed $data [, integer $lifetime = NULL , array $tags = NULL ] ) (在 Cache_Sqlite 中定义)

Set a value based on an id. Optionally add tags.

参数

  • string $id required - Id
  • mixed $data required - Data
  • integer $lifetime = NULL - Lifetime [Optional]
  • array $tags = NULL - Tags [Optional]

Tags

返回值

  • boolean

源代码

public function set_with_tags($id, $data, $lifetime = NULL, array $tags = NULL)
{
	// Serialize the data
	$data = serialize($data);
	// Normalise tags
	$tags = (NULL === $tags) ? NULL : ('<' . implode('>,<', $tags) . '>');
	// Setup lifetime
	if ($lifetime === NULL)
	{
		$lifetime = (0 === Arr::get('default_expire', NULL)) ? 0 : (Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE) + time());
	}
	else
	{
		$lifetime = (0 === $lifetime) ? 0 : ($lifetime + time());
	}
	// Prepare statement
	// $this->exists() may throw Cache_Exception, no need to catch/rethrow
	$statement = $this->exists($id) ? $this->_db->prepare('UPDATE caches SET expiration = :expiration, cache = :cache, tags = :tags WHERE id = :id') : $this->_db->prepare('INSERT INTO caches (id, cache, expiration, tags) VALUES (:id, :cache, :expiration, :tags)');
	// Try to insert
	try
	{
		$statement->execute(array(':id' => $this->_sanitize_id($id), ':cache' => $data, ':expiration' => $lifetime, ':tags' => $tags));
	}
	catch (PDOException $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	return (bool) $statement->rowCount();
}

final public __clone( ) (在 Cache 中定义)

Overload the __clone() method to prevent cloning

Tags

返回值

  • void

源代码

final public function __clone()
{
	throw new Cache_Exception('Cloning of BootPHP_Cache objects is forbidden');
}

public config( [ mixed $key = NULL , mixed $value = NULL ] ) (在 Cache 中定义)

Getter and setter for the configuration. If no argument provided, the current configuration is returned. Otherwise the configuration is set to this class.

// Overwrite all configuration
$cache->config(array('driver' => 'memcache', '...'));

// Set a new configuration setting
$cache->config('servers', array(
    'foo' => 'bar',
    '...'
    ));

// Get a configuration setting
$servers = $cache->config('servers);

参数

  • mixed $key = NULL - Key to set to array, either array or config path
  • mixed $value = NULL - Value to associate with key

返回值

  • mixed

源代码

public function config($key = NULL, $value = NULL)
{
	if ($key === NULL)
		return $this->_config;
	if (is_array($key))
	{
		$this->_config = $key;
	}
	else
	{
		if ($value === NULL)
			return Arr::get($this->_config, $key);
		$this->_config[$key] = $value;
	}
	return $this;
}

public static instance( [ string $group = NULL ] ) (在 Cache 中定义)

Creates a singleton of a BootPHP Cache group. If no group is supplied the default cache group is used.

// Create an instance of the default group
$default_group = Cache::instance();

// Create an instance of a group
$foo_group = Cache::instance('foo');

// Access an instantiated group directly
$foo_group = Cache::$instances['default'];

参数

  • string $group = NULL - The name of the cache group to use [Optional]

Tags

返回值

  • BootPHP_Cache

源代码

public static function instance($group = NULL)
{
	// If there is no group supplied
	if ($group === NULL)
	{
		// Use the default setting
		$group = Cache::$default;
	}
	if (isset(Cache::$instances[$group]))
	{
		// Return the current group if initiated already
		return Cache::$instances[$group];
	}
	$config = BootPHP::$config->load('cache');
	if (!$config->offsetExists($group))
	{
		throw new Cache_Exception(
		'Failed to load BootPHP Cache group: :group', array(':group' => $group)
		);
	}
	$config = $config->get($group);
	// Create a new cache type instance
	$cache_class = 'Cache_' . ucfirst($config['driver']);
	Cache::$instances[$group] = new $cache_class($config);
	// Return the instance
	return Cache::$instances[$group];
}

protected __construct( array $config ) (在 Cache_Sqlite 中定义)

设置 PDO SQLite 表,并初始化 PDO 连接

参数

  • array $config required - 配置

Tags

源代码

protected function __construct(array $config)
{
	parent::__construct($config);
	$database = Arr::get($this->_config, 'database', NULL);
	if ($database === NULL)
	{
		throw new Cache_Exception('Database path not available in BootPHP Cache configuration');
	}
	// 加载新的 Sqlite DB
	$this->_db = new PDO('sqlite:' . $database);
	// 测试 DB 是否存在
	$result = $this->_db->query("SELECT * FROM sqlite_master WHERE name = 'caches' && type = 'table'")->fetchAll();
	// 如果没有表,则创建一个新的
	if (0 == count($result))
	{
		$database_schema = Arr::get($this->_config, 'schema', NULL);
		if ($database_schema === NULL)
		{
			throw new Cache_Exception('Database schema not found in BootPHP Cache configuration');
		}
		try
		{
			// 创建缓存表
			$this->_db->query(Arr::get($this->_config, 'schema', NULL));
		}
		catch (PDOException $e)
		{
			throw new Cache_Exception('Failed to create new SQLite caches table with the following error : :error', array(':error' => $e->getMessage()));
		}
	}
}

protected exists( string $id ) (在 Cache_Sqlite 中定义)

Tests whether an id exists or not

参数

  • string $id required - Id

Tags

返回值

  • boolean

源代码

protected function exists($id)
{
	$statement = $this->_db->prepare('SELECT id FROM caches WHERE id = :id');
	try
	{
		$statement->execute(array(':id' => $this->_sanitize_id($id)));
	}
	catch (PDOExeption $e)
	{
		throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
	}
	return (bool) $statement->fetchAll();
}

protected _sanitize_id( string $id ) (在 Cache 中定义)

Replaces troublesome characters with underscores.

// Sanitize a cache id
$id = $this->_sanitize_id($id);

参数

  • string $id required - Id of cache to sanitize

返回值

  • string

源代码

protected function _sanitize_id($id)
{
	// Change slashes and spaces to underscores
	return str_replace(array('/', '\\', ' '), '_', $id);
}