|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rikudou\DynamoDbCache\Converter; |
| 4 | + |
| 5 | +use AsyncAws\DynamoDb\DynamoDbClient; |
| 6 | +use AsyncAws\DynamoDb\Input\BatchWriteItemInput; |
| 7 | +use AsyncAws\DynamoDb\ValueObject\AttributeValue; |
| 8 | +use AsyncAws\DynamoDb\ValueObject\PutRequest; |
| 9 | +use AsyncAws\DynamoDb\ValueObject\WriteRequest; |
| 10 | +use Psr\Cache\CacheItemInterface; |
| 11 | +use Ramsey\Uuid\Uuid; |
| 12 | +use Rikudou\DynamoDbCache\Converter\CacheItemConverterInterface; |
| 13 | +use Rikudou\DynamoDbCache\DynamoCacheItem; |
| 14 | +use Rikudou\DynamoDbCache\DynamoPartitionItem; |
| 15 | + |
| 16 | +final class PartitionItemConverter implements CacheItemConverterInterface |
| 17 | +{ |
| 18 | + private const MAX_ITEM_SIZE_IN_BYTES = 400 * 1_024; // 400 KB |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + private CacheItemConverterInterface $converter, |
| 22 | + private DynamoDbClient $client, |
| 23 | + private string $tableName, |
| 24 | + private string $primaryField, |
| 25 | + private string $ttlField, |
| 26 | + private string $valueField, |
| 27 | + private bool $valueFieldBinary, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function supports(CacheItemInterface $cacheItem): bool |
| 32 | + { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + public function convert(CacheItemInterface $cacheItem): DynamoCacheItem |
| 37 | + { |
| 38 | + $cacheItem = $this->converter->convert($cacheItem); |
| 39 | + $cacheItemSize = $this->calculateItemSizeInBytes($cacheItem); |
| 40 | + |
| 41 | + if ($cacheItemSize <= self::MAX_ITEM_SIZE_IN_BYTES) { |
| 42 | + return $cacheItem; // partitioning not needed |
| 43 | + } |
| 44 | + |
| 45 | + $maxValueSize = self::MAX_ITEM_SIZE_IN_BYTES |
| 46 | + - ($cacheItemSize - strlen($cacheItem->getRaw())) // cache item size without value |
| 47 | + - strlen(DynamoPartitionItem::formatKey('', method_exists(Uuid::class, 'uuid7') ? '00000000-0000-0000-0000-000000000000' : '00')) // partition key without prefix |
| 48 | + ; |
| 49 | + |
| 50 | + if ($maxValueSize <= 0) { |
| 51 | + return $cacheItem; // not enough bytes left for partitioning |
| 52 | + } |
| 53 | + |
| 54 | + $partitionValues = str_split($cacheItem->getRaw(), $maxValueSize); |
| 55 | + $partitionKeys = array_map( |
| 56 | + fn (int $partitionKey) => method_exists(Uuid::class, 'uuid7') ? (string) Uuid::uuid7() : sprintf('%02d', $partitionKey), |
| 57 | + array_keys($partitionValues), |
| 58 | + ); |
| 59 | + |
| 60 | + $cacheItemExpiresAt = $cacheItem->getExpiresAt()?->getTimestamp(); |
| 61 | + $batchWriteItemOutput = $this->client->batchWriteItem(new BatchWriteItemInput([ |
| 62 | + 'RequestItems' => [ |
| 63 | + $this->tableName => array_map( |
| 64 | + fn (string $partitionValue, string $partitionKey) => new WriteRequest([ |
| 65 | + 'PutRequest' => new PutRequest([ |
| 66 | + 'Item' => [ |
| 67 | + $this->primaryField => new AttributeValue([ |
| 68 | + 'S' => DynamoPartitionItem::formatKey($cacheItem->getKey(), $partitionKey), |
| 69 | + ]), |
| 70 | + $this->valueField => new AttributeValue([ |
| 71 | + $this->valueFieldBinary ? 'B' : 'S' => $partitionValue, |
| 72 | + ]), |
| 73 | + ] + ($cacheItemExpiresAt !== null ? [ |
| 74 | + $this->ttlField => new AttributeValue([ |
| 75 | + 'N' => (string) $cacheItemExpiresAt, |
| 76 | + ]), |
| 77 | + ] : []), |
| 78 | + ]), |
| 79 | + ]), |
| 80 | + $partitionValues, |
| 81 | + $partitionKeys, |
| 82 | + ), |
| 83 | + ], |
| 84 | + ])); |
| 85 | + |
| 86 | + if ($batchWriteItemOutput->getUnprocessedItems() !== []) { |
| 87 | + return $cacheItem; |
| 88 | + } |
| 89 | + |
| 90 | + return $cacheItem->set(new DynamoPartitionItem($cacheItem->getKey(), $partitionKeys)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/CapacityUnitCalculations.html |
| 95 | + * @see https://github.com/zaccharles/dynamodb-calculator |
| 96 | + */ |
| 97 | + private function calculateItemSizeInBytes(DynamoCacheItem $cacheItem): int |
| 98 | + { |
| 99 | + $size = strlen($this->primaryField) + strlen($cacheItem->getKey()); |
| 100 | + $size += strlen($this->valueField) + strlen($cacheItem->getRaw()); |
| 101 | + $expiresAt = $cacheItem->getExpiresAt(); |
| 102 | + |
| 103 | + if ($expiresAt !== null) { |
| 104 | + // The size of a number is approximately (number of UTF-8-encoded bytes of attribute name) + (1 byte per two significant digits) + (1 byte). |
| 105 | + $size += strlen($this->ttlField) + (int) ceil(strlen((string) $expiresAt->getTimestamp()) / 2) + 1; |
| 106 | + } |
| 107 | + |
| 108 | + return $size; |
| 109 | + } |
| 110 | +} |
0 commit comments