Skip to content

Commit a5de360

Browse files
authored
cacheable - chore: updating readme around cacheable sync (#1378)
* cacheable - chore: updating readme around cacheable sync * test fixes
1 parent 64fedcd commit a5de360

2 files changed

Lines changed: 15 additions & 35 deletions

File tree

packages/cacheable/README.md

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,11 @@ const cache = new Cacheable({secondary, nonBlocking: true});
365365

366366
## How It Works
367367

368-
CacheSync uses message providers from Qified to broadcast cache operations (SET and DELETE) to all connected cache instances. Each instance subscribes to these events and automatically updates its local storage when receiving updates from other instances.
368+
`CacheableSync` uses message providers from Qified to broadcast cache operations (SET and DELETE) to all connected cache instances. Each instance subscribes to these events and automatically updates its `primary` (example: in-memory) storage when receiving updates from other instances.
369369

370370
## Supported Message Providers
371371

372-
CacheSync supports all Qified message providers including:
373-
374-
* **Redis** - `@qified/redis` - Redis Pub/Sub
375-
* **RabbitMQ** - `@qified/rabbitmq` - RabbitMQ message broker
376-
* **NATS** - `@qified/nats` - NATS messaging system
372+
`Qified` supports multiple providers and you can learn more by going to https://qified.org.
377373

378374
## Basic Usage
379375

@@ -398,6 +394,8 @@ const cache2 = new Cacheable({
398394
// Set a value in cache1
399395
await cache1.set('key', 'value');
400396

397+
// Note: you might want to sleep for a bit based on the backend.
398+
401399
// The value is automatically synced to cache2
402400
const value = await cache2.get('key'); // Returns 'value'
403401
```
@@ -444,46 +442,25 @@ const cache = new Cacheable({
444442
});
445443
```
446444

447-
## Programmatically Setting Sync
448-
449-
You can also set the sync property after creating a cache instance:
450-
451-
```javascript
452-
import { Cacheable, CacheableSync } from 'cacheable';
453-
import { RedisMessageProvider } from '@qified/redis';
454-
455-
const cache = new Cacheable();
456-
457-
const provider = new RedisMessageProvider({
458-
connection: { host: 'localhost', port: 6379 }
459-
});
460-
461-
const sync = new CacheableSync({ qified: provider });
462-
cache.sync = sync;
463-
```
464-
465445
## How Sync Works
466446

467-
When sync is enabled:
468-
469447
1. **SET Operations**: When you call `cache.set()` or `cache.setMany()`, the cache:
470-
- Updates the local primary storage
448+
- Updates the local primary storage and secondary storage
471449
- Publishes a `cache:set` event with the key, value, ttl, and cacheId
472-
- Other cache instances receive the event and update their local storage (excluding the originating instance)
450+
- Other cache instances receive the event and update their `primary` storage (excluding the originating instance)
473451

474452
2. **DELETE Operations**: When you call `cache.delete()` or `cache.deleteMany()`, the cache:
475-
- Removes the key from local primary storage
453+
- Removes the key from primary and secondary storage
476454
- Publishes a `cache:delete` event with the key and cacheId
477455
- Other cache instances receive the event and remove the key from their storage
478456

479-
3. **Instance Filtering**: Each cache instance has a unique `cacheId`. Events are only applied if they come from a different instance, preventing infinite loops.
480-
481457
## Important Notes
482458

483-
* Cache sync only works with the **primary storage layer**. Secondary storage is not synchronized.
484-
* Each cache instance should have a unique `cacheId` to properly filter sync events.
459+
* Cache sync only works with the **primary storage layer**. Secondary storage is usually handled by the instance doing the initial work.
460+
* Each cache instance should have a unique `cacheId` to properly filter sync events. This is setup by default but you can set it if you want.
485461
* Sync events are **eventually consistent** - there may be a small delay between when a value is set and when it appears in other instances.
486-
* The sync feature requires a message provider to be running and accessible by all cache instances.
462+
* The sync feature requires a message provider to be running and accessible by all cache instances.
463+
* Each cache instance has a unique `cacheId`. Events are only applied if they come from a different instance, preventing infinite loops.
487464

488465
# Cacheable Options
489466

packages/cacheable/test/index.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,14 @@ describe("cacheable options and properties", async () => {
225225
},
226226
});
227227

228+
// Wait for subscription to be ready
229+
await new Promise((resolve) => setTimeout(resolve, 100));
230+
228231
await cacheable.set("deleteKey", "value");
229232
await cacheable.delete("deleteKey");
230233

231234
// Wait for message to be received
232-
await new Promise((resolve) => setTimeout(resolve, 100));
235+
await new Promise((resolve) => setTimeout(resolve, 200));
233236

234237
expect(receivedMessage).toBeDefined();
235238
expect(receivedMessage?.data.cacheId).toBe(cacheable.cacheId);

0 commit comments

Comments
 (0)