|
| 1 | +# BEAR.QueryRepository |
| 2 | + |
| 3 | +> Distributed caching framework for BEAR.Sunday applications using CQRS pattern |
| 4 | + |
| 5 | +BEAR.QueryRepository separates query (read) and command (write) operations to optimize caching and performance. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Event-driven cache invalidation |
| 10 | +- Automatic dependency resolution between cached resources |
| 11 | +- Donut caching (combining dynamic and static content) |
| 12 | +- CDN integration (Fastly, Akamai) |
| 13 | +- ETag support for conditional requests |
| 14 | +- Redis and Memcached support |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require bear/query-repository |
| 20 | +``` |
| 21 | + |
| 22 | +## Attributes |
| 23 | + |
| 24 | +### #[Cacheable] |
| 25 | + |
| 26 | +Marks a resource as cacheable with TTL-based expiration. |
| 27 | + |
| 28 | +```php |
| 29 | +use BEAR\RepositoryModule\Annotation\Cacheable; |
| 30 | + |
| 31 | +#[Cacheable(expiry: 'short')] // 60 seconds |
| 32 | +#[Cacheable(expiry: 'medium')] // 1 hour |
| 33 | +#[Cacheable(expiry: 'long')] // 1 day |
| 34 | +#[Cacheable(expiry: 'never')] // 1 year |
| 35 | +#[Cacheable(expirySecond: 300)] // Custom TTL |
| 36 | +``` |
| 37 | + |
| 38 | +Parameters: |
| 39 | +- `expiry`: 'short' | 'medium' | 'long' | 'never' |
| 40 | +- `expirySecond`: Custom TTL in seconds |
| 41 | +- `expiryAt`: Body field containing expiry timestamp |
| 42 | +- `type`: 'value' (body only) | 'view' (body + rendered view) |
| 43 | + |
| 44 | +### #[HttpCache] |
| 45 | + |
| 46 | +Controls HTTP Cache-Control headers. |
| 47 | + |
| 48 | +```php |
| 49 | +use BEAR\RepositoryModule\Annotation\HttpCache; |
| 50 | + |
| 51 | +#[HttpCache(maxAge: 60, sMaxAge: 600)] |
| 52 | +#[HttpCache(isPrivate: true, maxAge: 300)] |
| 53 | +#[HttpCache(noCache: true, mustRevalidate: true)] |
| 54 | +``` |
| 55 | + |
| 56 | +Parameters: |
| 57 | +- `isPrivate`: Private vs public cache |
| 58 | +- `noCache`: Force revalidation |
| 59 | +- `noStore`: Disable storage |
| 60 | +- `mustRevalidate`: Require revalidation when stale |
| 61 | +- `maxAge`: Client cache lifetime (seconds) |
| 62 | +- `sMaxAge`: Shared cache (CDN) lifetime (seconds) |
| 63 | + |
| 64 | +### #[NoHttpCache] |
| 65 | + |
| 66 | +Shorthand for disabling HTTP caching. |
| 67 | + |
| 68 | +```php |
| 69 | +use BEAR\RepositoryModule\Annotation\NoHttpCache; |
| 70 | + |
| 71 | +#[NoHttpCache] // Outputs: private, no-store, no-cache, must-revalidate |
| 72 | +``` |
| 73 | + |
| 74 | +### #[Refresh] |
| 75 | + |
| 76 | +Invalidates dependent cache after command execution. |
| 77 | + |
| 78 | +```php |
| 79 | +use BEAR\RepositoryModule\Annotation\Refresh; |
| 80 | + |
| 81 | +#[Refresh(uri: 'app://self/user')] |
| 82 | +#[Refresh(uri: 'app://self/user/{id}')] // URI template |
| 83 | +public function onPut(int $id): static { ... } |
| 84 | +``` |
| 85 | + |
| 86 | +### #[Purge] |
| 87 | + |
| 88 | +Purges specific cache after command execution. |
| 89 | + |
| 90 | +```php |
| 91 | +use BEAR\RepositoryModule\Annotation\Purge; |
| 92 | + |
| 93 | +#[Purge(uri: 'app://self/user/{id}')] |
| 94 | +public function onDelete(int $id): static { ... } |
| 95 | +``` |
| 96 | + |
| 97 | +## Modules |
| 98 | + |
| 99 | +### StorageRedisDsnModule |
| 100 | + |
| 101 | +Configures Redis-backed storage. |
| 102 | + |
| 103 | +```php |
| 104 | +use BEAR\QueryRepository\StorageRedisDsnModule; |
| 105 | + |
| 106 | +new StorageRedisDsnModule('redis://localhost:6379'); |
| 107 | +``` |
| 108 | + |
| 109 | +### StorageMemcachedModule |
| 110 | + |
| 111 | +Configures Memcached-backed storage. |
| 112 | + |
| 113 | +```php |
| 114 | +use BEAR\QueryRepository\StorageMemcachedModule; |
| 115 | + |
| 116 | +new StorageMemcachedModule('localhost', 11211); |
| 117 | +``` |
| 118 | + |
| 119 | +### CDN Modules |
| 120 | + |
| 121 | +```php |
| 122 | +use BEAR\QueryRepository\FastlyModule; |
| 123 | +use BEAR\QueryRepository\AkamaiModule; |
| 124 | + |
| 125 | +new FastlyModule($apiKey, $serviceId); |
| 126 | +new AkamaiModule($host, $clientToken, $clientSecret, $accessToken); |
| 127 | +``` |
| 128 | + |
| 129 | +## Cache Dependencies |
| 130 | + |
| 131 | +Resources automatically track dependencies. When resource A embeds resource B, invalidating B also invalidates A. |
| 132 | + |
| 133 | +```php |
| 134 | +#[Cacheable] |
| 135 | +class BlogPost extends ResourceObject |
| 136 | +{ |
| 137 | + public function onGet(int $id): static |
| 138 | + { |
| 139 | + $this->body = [ |
| 140 | + 'post' => $this->getPost($id), |
| 141 | + 'comments' => $this->resource->get('app://self/comments', ['post_id' => $id]), |
| 142 | + ]; |
| 143 | + return $this; |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +When comments are updated, BlogPost cache is automatically invalidated via surrogate keys. |
| 149 | + |
| 150 | +## Donut Caching |
| 151 | + |
| 152 | +Combines static outer content with dynamic inner content. |
| 153 | + |
| 154 | +```php |
| 155 | +use BEAR\RepositoryModule\Annotation\DonutCache; |
| 156 | + |
| 157 | +#[DonutCache] |
| 158 | +class BlogPage extends ResourceObject |
| 159 | +{ |
| 160 | + public function onGet(): static |
| 161 | + { |
| 162 | + $this->body = [ |
| 163 | + 'header' => $this->resource->get('app://self/header'), // static |
| 164 | + 'content' => $this->resource->get('app://self/content'), // dynamic |
| 165 | + ]; |
| 166 | + return $this; |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## Repository Logger |
| 172 | + |
| 173 | +Cache operations are logged in JSON format for debugging and monitoring. |
| 174 | + |
| 175 | +```json |
| 176 | +{"op":"save-value","uri":"page://self/user","tags":["etag123","_user_"],"ttl":3600} |
| 177 | +{"op":"invalidate-etag","tags":["_user_"]} |
| 178 | +{"op":"put-query-repository","uri":"app://self/posts"} |
| 179 | +``` |
| 180 | + |
| 181 | +Schema: https://bearsunday.github.io/BEAR.QueryRepository/schemas/repository-log.json |
| 182 | + |
| 183 | +## Architecture |
| 184 | + |
| 185 | +### Core Components |
| 186 | + |
| 187 | +- **QueryRepository**: Main entry point for cache operations (put, get, purge) |
| 188 | +- **ResourceStorage**: Low-level storage using Symfony Cache TagAwareAdapter |
| 189 | +- **CacheInterceptor**: AOP interceptor for #[Cacheable] resources |
| 190 | +- **CommandInterceptor**: Handles #[Refresh] and #[Purge] after commands |
| 191 | +- **DonutRepository**: Implements donut caching pattern |
| 192 | +- **CacheDependency**: Manages cache dependencies via Surrogate-Key headers |
| 193 | + |
| 194 | +### Cache Invalidation Flow |
| 195 | + |
| 196 | +1. Resource with #[Refresh] or #[Purge] is invoked |
| 197 | +2. CommandInterceptor executes the command |
| 198 | +3. After successful response, annotations are processed |
| 199 | +4. ResourceStorage.invalidateTags() clears both RO and ETag pools |
| 200 | +5. CDN purger (if configured) purges surrogate keys |
| 201 | + |
| 202 | +## Documentation |
| 203 | + |
| 204 | +- Manual: https://bearsunday.github.io/manuals/1.0/en/cache.html |
| 205 | +- Repository: https://github.com/bearsunday/BEAR.QueryRepository |
| 206 | + |
| 207 | +## Schemas |
| 208 | + |
| 209 | +- Repository Log Format: https://bearsunday.github.io/BEAR.QueryRepository/schemas/repository-log.json |
0 commit comments