mouf/utils.cache.cache-interface

该包仅包含缓存类必须实现接口。除非您想实现自己的缓存方法,否则您应该导入使用此接口的缓存包。例如,common.utils.session-cache,或common.utils.file-cache。

v2.1.0 2018-06-20 09:45 UTC

This package is auto-updated.

Last update: 2024-09-15 04:54:46 UTC


README

Mouf 框架只是一个 IOC 框架。因此,它不提供管理任何类型缓存的方法。希望 Mouf 团队也提供一系列用于管理对象缓存的包。

缓存架构

Mouf 提供了几个缓存的实现,如果您愿意,可以提供自己的实现。每个缓存机制必须仅扩展包 utils/cache/cacheinterface 中的 CacheInterface 接口。

默认情况下,Mouf 提供以下缓存

  • FileCache:将缓存元素写入文件的缓存,在临时文件夹中。
  • SessionCache:将缓存元素写入当前用户的会话中。因此,此缓存是用户本地的。
  • ApcCache:使用 APC 扩展来存储数据的缓存。
  • NoCache:不提供任何缓存的缓存。在开发过程中可能很有用。

缓存方法

每个实现 CacheInterface 的类提供简单的方法来获取和设置缓存中的数据

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();
}

例如,要将一些值存储在缓存中,只需写入

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

这将把值 "myvalue" 存储在缓存中,键为 "mykey"。该值将存储 15 秒。如果在 15 秒内我们调用检索缓存,将检索该值

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

第三个参数是可选的。如果未传递,则使用默认值。默认值是 "forever",表示缓存的值永远不会超时。默认值可以在 Mouf 缓存实例属性中覆盖。