Skip to content

Commit

Permalink
v4.0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
joeservd committed Dec 11, 2024
1 parent ce52bfe commit ba9cc65
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Servd Assets and Helpers

## 4.0.12 - 2024-12-11

### Updated

- Reduced the cache memory usage of the "Automated Tag Based Purge" option for the "Cache Clear Strategy" plugin field.

## 4.0.11 - 2024-12-03

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "servd/craft-asset-storage",
"description": "Servd Asset Storage and Helpers integration for Craft CMS",
"version": "4.0.11",
"version": "4.0.12",
"type": "craft-plugin",
"keywords": [
"cms",
Expand Down
65 changes: 38 additions & 27 deletions src/StaticCache/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use Craft;
use craft\base\Component;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Redis;

class Tags extends Component
{
const TAG_PREFIX = 'svd-tag-';
const URL_PREFIX = 'svd-url-';
const URL_HASH_LOOKUP = 'svd-url-lookup';
const TAG_PREFIX = 'svd-t-';
const URL_PREFIX = 'svd-u-';
const SECTION_ID_PREFIX = 'sec';
const ELEMENT_ID_PREFIX = 'el';
const GLOBAL_SET_PREFIX = 'gs';
const STRUCTURE_ID_PREFIX = 'st';
const VOLUME_ID_PREFIX = 'vo';
const MD5_LENGTH = 10;

const IGNORE_TAGS_FROM_CLASSES = [
'craft\elements\MatrixBlock',
Expand All @@ -25,20 +26,8 @@ class Tags extends Component

private static $redis = null;


public $tags = [];

private function getRedisConnection()
{
if (static::$redis === null) {
$redisDb = intval(getenv('REDIS_STATIC_CACHE_DB'));
static::$redis = new Redis();
static::$redis->connect(getenv('REDIS_HOST'), getenv('REDIS_PORT'));
static::$redis->select($redisDb);
}
return static::$redis;
}

public function addTagForCurrentRequest($tag)
{
$this->tags[] = $tag;
Expand All @@ -53,17 +42,19 @@ public function associateCurrentRequestTagsWithUrl($url)
{
Craft::beginProfile('Tags::associateCurrentRequestTagsWithUrl', __METHOD__);
$url = $this->normaliseUrl($url);
$urlMd5 = md5($url);
$urlHash = substr(md5($url), 0, static::MD5_LENGTH);

$uniqueTags = $this->getAllTagsForCurrentRequest();

try {
$redis = $this->getRedisConnection();
$redisBatch = $redis->multi(Redis::PIPELINE);
$redis->hSet(static::URL_HASH_LOOKUP, $urlHash, $url);

foreach ($uniqueTags as $tag) {
$redisBatch->sAdd(static::TAG_PREFIX . $tag, $url);
$redisBatch->sAdd(static::TAG_PREFIX . $tag, $urlHash);
}
$redisBatch->sAddArray(static::URL_PREFIX . $urlMd5, $uniqueTags);
$redisBatch->sAddArray(static::URL_PREFIX . $urlHash, $uniqueTags);
$redisBatch->exec();
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
Expand All @@ -76,7 +67,11 @@ public function getUrlsForTag($tag)
{
try {
$redis = $this->getRedisConnection();
$urls = $redis->sMembers(static::TAG_PREFIX . $tag);
$urlHashes = $redis->sMembers(static::TAG_PREFIX . $tag);
$urls = [];
foreach ($urlHashes as $hash) {
$urls[] = 'http://' . $redis->hGet(static::URL_HASH_LOOKUP, $hash);
}
return $urls;
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
Expand All @@ -94,7 +89,9 @@ public function iterateUrlsForTag($tag, $callback)
$it = NULL;
while (($arr_mems = $redis->sScan(static::TAG_PREFIX . $tag, $it)) && $counter < $totalSetSize) {
$counter += sizeof($arr_mems);
$callback($arr_mems);
$callback(array_map(function($hash) use ($redis) {
return 'http://' . $redis->hGet(static::URL_HASH_LOOKUP, $hash);
}, $arr_mems));
}
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
Expand All @@ -104,26 +101,40 @@ public function iterateUrlsForTag($tag, $callback)
public function clearTagsForUrl($url)
{
$url = $this->normaliseUrl($url);
$urlMd5 = md5($url);
$urlHash = substr(md5($url), 0, static::MD5_LENGTH);

try {
$redis = $this->getRedisConnection();
//Get all tags for the url
$tags = $redis->sMembers(static::URL_PREFIX . $urlMd5);
$tags = $redis->sMembers(static::URL_PREFIX . $urlHash);
$redisBatch = $redis->multi(Redis::PIPELINE);
foreach ($tags as $tag) {
//Clear the tag -> url association
$redisBatch->sRem(static::TAG_PREFIX . $tag, $url);
$redisBatch->sRem(static::TAG_PREFIX . $tag, $urlHash);
}
//Clear the url -> tags associations
$redisBatch->unlink(static::URL_PREFIX . $urlMd5);
$redisBatch->unlink(static::URL_PREFIX . $urlHash);
$redisBatch->hDel(static::URL_HASH_LOOKUP, $urlHash);
$redisBatch->exec();
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
}
}

public function normaliseUrl($url)
private function getRedisConnection()
{
if (static::$redis === null) {
$redisDb = intval(getenv('REDIS_STATIC_CACHE_DB'));
static::$redis = new Redis();
static::$redis->connect(getenv('REDIS_HOST'), getenv('REDIS_PORT'));
static::$redis->select($redisDb);
}
return static::$redis;
}

private function normaliseUrl($url)
{
return str_ireplace('https://', 'http://', $url);
$url = str_ireplace('https://', '', $url);
return str_ireplace('http://', '', $url);
}
}
}

0 comments on commit ba9cc65

Please sign in to comment.