Skip to content

Commit 26bba5e

Browse files
committed
documentation
1 parent 3f1f399 commit 26bba5e

3 files changed

Lines changed: 97 additions & 16 deletions

File tree

cache/docs/DETAILED.md

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ These are typically pre-installed on Linux/macOS systems. If missing, install vi
4040

4141
### Default Settings
4242
- **Enabled by default**: Set `CACHING=false` to disable
43-
- **Max Length**: 1000 entries
44-
- **Max Bytes**: 1GB (1,000,000,000 bytes)
45-
- **TTL (Time-To-Live)**: 5 minutes (300,000ms)
43+
- **Max Length**: 1000 entries (configurable)
44+
- **Max Bytes**: 1GB (1,000,000,000 bytes) (configurable)
45+
- **TTL (Time-To-Live)**: 5 minutes default, 24 hours in production (300,000ms or 86,400,000ms)
4646
- **Eviction Policy**: LRU (Least Recently Used)
4747
- **Storage**: In-memory (per server instance)
4848

@@ -51,7 +51,7 @@ These are typically pre-installed on Linux/macOS systems. If missing, install vi
5151
CACHING=true # Enable/disable caching layer (true/false)
5252
CACHE_MAX_LENGTH=1000 # Maximum number of cached entries
5353
CACHE_MAX_BYTES=1000000000 # Maximum memory usage in bytes
54-
CACHE_TTL=300000 # Time-to-live in milliseconds
54+
CACHE_TTL=300000 # Time-to-live in milliseconds (300000 = 5 min, 86400000 = 24 hr)
5555
```
5656

5757
### Enabling/Disabling Cache
@@ -348,12 +348,48 @@ Clears all cache entries:
348348

349349
When write operations occur, the cache middleware intercepts the response and invalidates relevant cache entries based on the object properties.
350350

351+
**MongoDB Operator Support**: The smart invalidation system supports complex MongoDB query operators, including:
352+
- **`$or`** - Matches if ANY condition is satisfied (e.g., queries checking multiple target variations)
353+
- **`$and`** - Matches if ALL conditions are satisfied
354+
- **`$exists`** - Field existence checking
355+
- **`$size`** - Array size matching (e.g., `{"__rerum.history.next": {"$exists": true, "$size": 0}}` for leaf objects)
356+
- **Comparison operators** - `$ne`, `$gt`, `$gte`, `$lt`, `$lte`
357+
- **`$in`** - Value in array matching
358+
- **Nested properties** - Dot notation like `target.@id`, `body.title.value`
359+
360+
**Protected Properties**: The system intelligently skips `__rerum` and `_id` fields during cache matching, as these are server-managed properties not present in user request bodies. This includes:
361+
- Top-level: `__rerum`, `_id`
362+
- Nested paths: `__rerum.history.next`, `target._id`, etc.
363+
- Any position: starts with, contains, or ends with these protected property names
364+
365+
This conservative approach ensures cache invalidation is based only on user-controllable properties, preventing false negatives while maintaining correctness.
366+
367+
**Example with MongoDB Operators**:
368+
```javascript
369+
// Complex query with $or operator (common in Annotation queries)
370+
{
371+
"body": {
372+
"$or": [
373+
{"target": "https://example.org/canvas/1"},
374+
{"target.@id": "https://example.org/canvas/1"}
375+
]
376+
},
377+
"__rerum.history.next": {"$exists": true, "$size": 0} // Skipped (protected)
378+
}
379+
380+
// When an Annotation is updated with target="https://example.org/canvas/1",
381+
// the cache system:
382+
// 1. Evaluates the $or operator against the updated object
383+
// 2. Skips the __rerum.history.next check (server-managed)
384+
// 3. Invalidates this cache entry if the $or condition matches
385+
```
386+
351387
### CREATE Invalidation
352388

353-
**Triggers**: `POST /v1/api/create`
389+
**Triggers**: `POST /v1/api/create`, `POST /v1/api/bulkCreate`
354390

355391
**Invalidates**:
356-
- All `query` caches where the new object matches the query filters
392+
- All `query` caches where the new object matches the query filters (with MongoDB operator support)
357393
- All `search` caches where the new object contains search terms
358394
- All `searchPhrase` caches where the new object contains the phrase
359395

@@ -366,13 +402,13 @@ When write operations occur, the cache middleware intercepts the response and in
366402

367403
### UPDATE Invalidation
368404

369-
**Triggers**: `PUT /v1/api/update`, `PATCH /v1/api/patch/*`
405+
**Triggers**: `PUT /v1/api/update`, `PUT /v1/api/bulkUpdate`, `PATCH /v1/api/patch`, `PATCH /v1/api/set`, `PATCH /v1/api/unset`, `PUT /v1/api/overwrite`
370406

371407
**Invalidates**:
372-
- The `id` cache for the updated object
373-
- All `query` caches matching the updated object's properties
408+
- The `id` cache for the updated object (and previous version in chain)
409+
- All `query` caches matching the updated object's properties (with MongoDB operator support)
374410
- All `search` caches matching the updated object's content
375-
- The `history` cache for all versions in the chain
411+
- The `history` cache for all versions in the chain (current, previous, prime)
376412
- The `since` cache for all versions in the chain
377413

378414
**Version Chain Logic**:
@@ -409,9 +445,55 @@ When write operations occur, the cache middleware intercepts the response and in
409445

410446
### PATCH Invalidation
411447

412-
**Triggers**: `PATCH /v1/api/patch/set`, `PATCH /v1/api/patch/unset`, `PATCH /v1/api/patch/update`
448+
**Triggers**:
449+
- `PATCH /v1/api/patch` - General property updates
450+
- `PATCH /v1/api/set` - Add new properties
451+
- `PATCH /v1/api/unset` - Remove properties
452+
453+
**Behavior**: Same as UPDATE invalidation (creates new version with MongoDB operator support)
454+
455+
**Note**: `PATCH /v1/api/release` does NOT use cache invalidation as it only modifies `__rerum` properties which are skipped during cache matching.
456+
457+
### OVERWRITE Invalidation
458+
459+
**Triggers**: `PUT /v1/api/overwrite`
460+
461+
**Behavior**: Similar to UPDATE but replaces entire object in place (same ID)
462+
463+
**Invalidates**:
464+
- The `id` cache for the overwritten object
465+
- All `query` caches matching the new object properties
466+
- All `search` caches matching the new object content
467+
- The `history` cache for all versions in the chain
468+
- The `since` cache for all versions in the chain
469+
470+
---
413471

414-
**Behavior**: Same as UPDATE invalidation (creates new version)
472+
## Write Endpoints with Smart Invalidation
473+
474+
All write operations that modify user-controllable properties have the `invalidateCache` middleware applied:
475+
476+
| Endpoint | Method | Middleware Applied | Invalidation Type |
477+
|----------|--------|-------------------|-------------------|
478+
| `/v1/api/create` | POST |`invalidateCache` | CREATE |
479+
| `/v1/api/bulkCreate` | POST |`invalidateCache` | CREATE (bulk) |
480+
| `/v1/api/update` | PUT |`invalidateCache` | UPDATE |
481+
| `/v1/api/bulkUpdate` | PUT |`invalidateCache` | UPDATE (bulk) |
482+
| `/v1/api/patch` | PATCH |`invalidateCache` | UPDATE |
483+
| `/v1/api/set` | PATCH |`invalidateCache` | UPDATE |
484+
| `/v1/api/unset` | PATCH |`invalidateCache` | UPDATE |
485+
| `/v1/api/overwrite` | PUT |`invalidateCache` | OVERWRITE |
486+
| `/v1/api/delete` | DELETE |`invalidateCache` | DELETE |
487+
488+
**Not Requiring Invalidation**:
489+
- `/v1/api/release` (PATCH) - Only modifies `__rerum` properties (server-managed, skipped in cache matching)
490+
491+
**Key Features**:
492+
- MongoDB operator support (`$or`, `$and`, `$exists`, `$size`, comparisons, `$in`)
493+
- Nested property matching (dot notation like `target.@id`)
494+
- Protected property handling (skips `__rerum` and `_id` fields)
495+
- Version chain invalidation for UPDATE/DELETE operations
496+
- Bulk operation support (processes multiple objects)
415497

416498
---
417499

cache/docs/SHORT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The RERUM API now includes an intelligent caching layer that significantly impro
3232
When you request data:
3333
1. **First request**: Fetches from database, caches result, returns data (~300-800ms)
3434
2. **Subsequent requests**: Returns cached data immediately (~1-5ms)
35-
3. **After 5 minutes**: Cache expires, next request refreshes from database
35+
3. **After TTL expires**: Cache entry removed, next request refreshes from database (default: 5 minutes, configurable up to 24 hours)
3636

3737
### For Write Operations
3838
When you create, update, or delete objects:
@@ -95,7 +95,7 @@ Cache behavior can be adjusted via environment variables:
9595
- `CACHING` - Enable/disable caching layer (default: `true`, set to `false` to disable)
9696
- `CACHE_MAX_LENGTH` - Maximum entries (default: 1000)
9797
- `CACHE_MAX_BYTES` - Maximum memory usage (default: 1GB)
98-
- `CACHE_TTL` - Time-to-live in milliseconds (default: 300000 = 5 minutes)
98+
- `CACHE_TTL` - Time-to-live in milliseconds (default: 300000 = 5 minutes, production often uses 86400000 = 24 hours)
9999

100100
**Note**: Limits are well-balanced for typical usage. With standard RERUM queries (100 items per page), 1000 cached entries use only ~26 MB (~2.7% of the 1GB byte limit). The byte limit serves as a safety net for edge cases.
101101

routes/release.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const router = express.Router()
44
//This controller will handle all MongoDB interactions.
55
import controller from '../db-controller.js'
66
import auth from '../auth/index.js'
7-
import { invalidateCache } from '../cache/middleware.js'
87

98
router.route('/:_id')
10-
.patch(auth.checkJwt, invalidateCache, controller.release)
9+
.patch(auth.checkJwt, controller.release)
1110
.all((req, res, next) => {
1211
res.statusMessage = 'Improper request method for releasing, please use PATCH to release this object.'
1312
res.status(405)

0 commit comments

Comments
 (0)