定制 mouf/utils.cache.cache-interface 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mouf/utils.cache.cache-interface

最新稳定版本:v2.1.0

Composer 安装命令:

composer require mouf/utils.cache.cache-interface

包简介

This package only contains the interface that must be implemented by caching classes. Unless you want to implement your own caching method, you should import a cache package that will use this interface. For instance, common.utils.session-cache, or common.utils.file-cache.

README 文档

README

The Mouf framework is only an IOC framework. As such, it does not provide any means for managing any kind of cache. Hopefully, the Mouf team provides also a range of packages to manage the caching of objects.

The cache architecture

Mouf provides several implementations of caching, and you can provide your own if you want. Each cache mechanism must just extend the CacheInterface interface that is part of the package utils/cache/cacheinterface.

By default, Mouf provides these caches:

  • FileCache: a cache that writes cached elements in files, in a temporary folder.
  • SessionCache: a cache that writes cached elements in the session of the current user. Therefore, this cache is local to a user.
  • ApcCache: a cache that uses the APC extension to store data.
  • NoCache: a cache that... does not provide any cache. Can be useful for development purpose.

The cache methods

Each class implementing the CacheInterface provides simple methods to get and set data in the cache:

interface CacheInterface {
	/**
	 * Returns the cached value for the key passed in parameter.
	 *
	 * @param string $key
	 * @return mixed
	 */
	function get($key);
	
	/**
	 * Sets the value in the cache.
	 *
	 * @param string $key The key of the value to store
	 * @param mixed $value The value to store
	 * @param int $timeToLive The time to live of the cache, in seconds.
	 */
	function set($key, $value, $timeToLive = null);
	
	/**
	 * Removes the object whose key is $key from the cache.
	 *
	 * @param string $key The key of the object
	 */
	function purge($key);
	
	/**
	 * Removes all the objects from the cache.
	 *
	 */
	function purgeAll();
}

For instance, to store some value in the cache, you just need to write:

$cache->set("mykey", "myvalue", 15);

This will store the value "myvalue" in the cache, with key "mykey". The value will be stored for 15 seconds. If within 15 seconds, we perform a call to retrieve the cache, the value will be retrieved:

// Will print "myvalue" if called within 15 seconds.
echo $cache->get("mykey");

The third parameter is optional. If not passed, the default value will be used. The default value is "forever", which means the cached value will never time out. The default value can be overidden in the Mouf cache instance properties.

统计信息

  • 总下载量: 378.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 7
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-10-16