Skip to content

Commit

Permalink
Add TagDependency. Up code from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
Falaleev Maxim committed Feb 5, 2015
1 parent 81b2511 commit 193bd32
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mindy/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function init()
* @param mixed $key the key to be normalized
* @return string the generated cache key
*/
protected function buildKey($key)
public function buildKey($key)
{
if (is_string($key)) {
$key = ctype_alnum($key) && mb_strlen($key, '8bit') <= 32 ? $key : md5($key);
Expand Down
122 changes: 122 additions & 0 deletions src/Mindy/Cache/TagDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* All rights reserved.
*
* @author Falaleev Maxim
* @email max@studio107.ru
* @version 1.0
* @company Studio107
* @site http://studio107.ru
* @date 05/02/15 18:27
*/

namespace Mindy\Cache;

/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* TagDependency associates a cached data item with one or multiple [[tags]].
*
* By calling [[invalidate()]], you can invalidate all cached data items that are associated with the specified tag name(s).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TagDependency extends Dependency
{
/**
* @var string|array a list of tag names for this dependency. For a single tag, you may specify it as a string.
*/
public $tags = [];


/**
* Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);

$newKeys = [];
foreach ($timestamps as $key => $timestamp) {
if ($timestamp === false) {
$newKeys[] = $key;
}
}
if (!empty($newKeys)) {
$timestamps = array_merge($timestamps, $this->touchKeys($cache, $newKeys));
}

return $timestamps;
}

/**
* Performs the actual dependency checking.
* @param Cache $cache the cache component that is currently evaluating this dependency
* @return boolean whether the dependency is changed or not.
*/
public function getHasChanged($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
return $timestamps !== $this->data;
}

/**
* Invalidates all of the cached data items that are associated with any of the specified [[tags]].
* @param Cache $cache the cache component that caches the data items
* @param string|array $tags
*/
public static function invalidate($cache, $tags)
{
$keys = [];
foreach ((array) $tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
static::touchKeys($cache, $keys);
}

/**
* Generates the timestamp for the specified cache keys.
* @param Cache $cache
* @param string[] $keys
* @return array the timestamp indexed by cache keys
*/
protected static function touchKeys($cache, $keys)
{
$items = [];
$time = microtime();
foreach ($keys as $key) {
$items[$key] = $time;
}
$cache->mset($items);
return $items;
}

/**
* Returns the timestamps for the specified tags.
* @param Cache $cache
* @param string[] $tags
* @return array the timestamps indexed by the specified tags.
*/
protected function getTimestamps($cache, $tags)
{
if (empty($tags)) {
return [];
}

$keys = [];
foreach ($tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}

return $cache->mget($keys);
}
}

0 comments on commit 193bd32

Please sign in to comment.