forked from neos/neos-development-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Use CacheTag to generate cache tags
- Loading branch information
Showing
9 changed files
with
391 additions
and
777 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Neos\Fusion\Cache; | ||
|
||
use Neos\ContentRepository\Core\Factory\ContentRepositoryId; | ||
use Neos\ContentRepository\Core\NodeType\NodeTypeName; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Node; | ||
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId; | ||
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId; | ||
use Neos\Flow\Annotations as Flow; | ||
|
||
/** | ||
* The cache tag value object | ||
*/ | ||
#[Flow\Proxy(false)] | ||
class CacheTag | ||
{ | ||
protected const PATTERN = '/^[a-zA-Z0-9_%\-&]{1,250}$/'; | ||
|
||
private function __construct( | ||
public readonly string $value | ||
) { | ||
} | ||
|
||
final public static function forNodeAggregate( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentStreamId $contentStreamId, | ||
NodeAggregateId $nodeAggregateId, | ||
): self { | ||
return new self( | ||
'Node_' | ||
. self::getHashForContentStreamIdAndContentRepositoryId($contentStreamId, $contentRepositoryId) | ||
. '_' . $nodeAggregateId->value | ||
|
||
); | ||
} | ||
|
||
final public static function forNodeAggregateFromNode(Node $node): self | ||
{ | ||
return self::forNodeAggregate( | ||
$node->subgraphIdentity->contentRepositoryId, | ||
$node->subgraphIdentity->contentStreamId, | ||
$node->nodeAggregateId | ||
); | ||
} | ||
|
||
final public static function forDescendantOfNode( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentStreamId $contentStreamId, | ||
NodeAggregateId $nodeAggregateId, | ||
): self { | ||
return new self( | ||
'DescendantOf_' | ||
. self::getHashForContentStreamIdAndContentRepositoryId($contentStreamId, $contentRepositoryId) | ||
. '_' . $nodeAggregateId->value | ||
); | ||
} | ||
|
||
final public static function forDescendantOfNodeFromNode(Node $node): self | ||
{ | ||
return self::forDescendantOfNode( | ||
$node->subgraphIdentity->contentRepositoryId, | ||
$node->subgraphIdentity->contentStreamId, | ||
$node->nodeAggregateId | ||
); | ||
} | ||
|
||
final public static function forAncestorNode( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentStreamId $contentStreamId, | ||
NodeAggregateId $nodeAggregateId, | ||
): self { | ||
return new self( | ||
'Ancestor' | ||
. self::getHashForContentStreamIdAndContentRepositoryId($contentStreamId, $contentRepositoryId) | ||
. '_' . $nodeAggregateId->value | ||
); | ||
} | ||
|
||
final public static function forAncestorNodeFromNode(Node $node): self | ||
{ | ||
return self::forAncestorNode( | ||
$node->subgraphIdentity->contentRepositoryId, | ||
$node->subgraphIdentity->contentStreamId, | ||
$node->nodeAggregateId | ||
); | ||
} | ||
|
||
final public static function forNodeTypeName( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentStreamId $contentStreamId, | ||
NodeTypeName $nodeTypeName, | ||
): self { | ||
return new self( | ||
'NodeType_' | ||
. self::getHashForContentStreamIdAndContentRepositoryId($contentStreamId, $contentRepositoryId) | ||
. '_' . \strtr($nodeTypeName->value, '.:', '_-') | ||
); | ||
} | ||
|
||
final public static function forDynamicNodeAggregate( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentStreamId $contentStreamId, | ||
NodeAggregateId $nodeAggregateId, | ||
): self { | ||
return new self( | ||
'DynamicNodeTag_' | ||
. self::getHashForContentStreamIdAndContentRepositoryId($contentStreamId, $contentRepositoryId) | ||
. '_' . $nodeAggregateId->value | ||
|
||
); | ||
} | ||
|
||
final public static function fromString(string $string): self | ||
{ | ||
if (preg_match(self::PATTERN, $string) !== 1) { | ||
throw new \InvalidArgumentException( | ||
'Given value "' . $string . '" is no valid cache tag, must match the defined pattern.', | ||
1658093413 | ||
); | ||
} | ||
|
||
return new self($string); | ||
} | ||
|
||
protected static function getHashForContentStreamIdAndContentRepositoryId( | ||
ContentStreamId $contentStreamId, | ||
ContentRepositoryId $contentRepositoryId, | ||
): string { | ||
return sha1($contentStreamId->value . '@' . $contentRepositoryId->value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Neos\Fusion\Cache; | ||
|
||
use Neos\ContentRepository\Core\Factory\ContentRepositoryId; | ||
use Neos\ContentRepository\Core\NodeType\NodeTypeName; | ||
use Neos\ContentRepository\Core\NodeType\NodeTypeNames; | ||
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId; | ||
use Neos\Flow\Annotations as Flow; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Nodes; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Node; | ||
|
||
/** | ||
* The cache tag value object set | ||
*/ | ||
#[Flow\Proxy(false)] | ||
final class CacheTagSet | ||
{ | ||
/** | ||
* Unique cache tags, indexed by their value | ||
* @var array<string,CacheTag> | ||
*/ | ||
private array $tags; | ||
|
||
public function __construct(CacheTag ...$tags) | ||
{ | ||
$uniqueTags = []; | ||
foreach ($tags as $tag) { | ||
$uniqueTags[$tag->value] = $tag; | ||
} | ||
|
||
$this->tags = $uniqueTags; | ||
} | ||
|
||
public static function forDescendantOfNodesFromNodes( | ||
Nodes $nodes | ||
): self { | ||
return new self(...array_map( | ||
fn(Node $node): CacheTag => CacheTag::forDescendantOfNodeFromNode( | ||
$node | ||
), | ||
iterator_to_array($nodes) | ||
)); | ||
} | ||
|
||
public static function forNodeAggregatesFromNodes( | ||
Nodes $nodes | ||
): self { | ||
return new self(...array_map( | ||
fn(Node $node): CacheTag => CacheTag::forNodeAggregateFromNode( | ||
$node | ||
), | ||
iterator_to_array($nodes) | ||
)); | ||
} | ||
|
||
|
||
public static function forNodeTypeNames( | ||
ContentRepositoryId $contentRepositoryId, | ||
ContentStreamId $contentStreamId, | ||
NodeTypeNames $nodeTypeNames | ||
): self { | ||
return new self(...array_map( | ||
fn(NodeTypeName $nodeTypeName): CacheTag => CacheTag::forNodeTypeName( | ||
$contentRepositoryId, | ||
$contentStreamId, | ||
$nodeTypeName | ||
), | ||
iterator_to_array($nodeTypeNames) | ||
)); | ||
} | ||
|
||
public function add(CacheTag $cacheTag): self | ||
{ | ||
$tags = $this->tags; | ||
$tags[$cacheTag->value] = $cacheTag; | ||
|
||
return new self(...$tags); | ||
} | ||
|
||
/** | ||
* @return array<int,string> | ||
*/ | ||
public function toStringArray(): array | ||
{ | ||
return array_map( | ||
fn(CacheTag $tag): string => $tag->value, | ||
array_values($this->tags) | ||
); | ||
} | ||
|
||
public function union(self $other): self | ||
{ | ||
return new self(...array_merge($this->tags, $other->tags)); | ||
} | ||
} |
Oops, something went wrong.