Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

cluster-shared-cache

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cluster-shared-cache - npm Package Compare versions

Comparing version
1.0.0
to
1.0.1
+1
-1
package.json
{
"name": "cluster-shared-cache",
"version": "1.0.0",
"version": "1.0.1",
"description": "Handling shared in memory cache in node js cluster module",

@@ -5,0 +5,0 @@ "main": "index.js",

const ClusterSharedCache = require('../src/index');
const cluster = require('cluster');
describe('ClusterSharedCache', () => {
let cache;
beforeEach(() => {
cache = new ClusterSharedCache({
maxSize: 10,
ttl: 1000,
checkPeriod: 500
});
});
afterEach(() => {
if (cache) {
cache.destroy();
}
});
describe('Basic Operations', () => {
test('should set and get values', async () => {
await cache.set('test1', 'value1');
const result = await cache.get('test1');
if (cluster.isMaster) {
expect(result).toBe('value1');
} else {
// In worker, we need to wait for sync
setTimeout(async () => {
const result = await cache.get('test1');
expect(result).toBe('value1');
}, 100);
}
});
test('should return undefined for non-existent keys', async () => {
const result = await cache.get('nonexistent');
expect(result).toBeUndefined();
});
test('should delete values', async () => {
await cache.set('test2', 'value2');
await cache.delete('test2');
const result = await cache.get('test2');
expect(result).toBeUndefined();
});
test('should clear all values', async () => {
await cache.set('test3', 'value3');
await cache.set('test4', 'value4');
await cache.clear();
const result1 = await cache.get('test3');
const result2 = await cache.get('test4');
expect(result1).toBeUndefined();
expect(result2).toBeUndefined();
});
});
describe('TTL Functionality', () => {
test('should expire values after TTL', (done) => {
cache.set('ttl_test', 'value', 200).then(() => {
setTimeout(async () => {
const result = await cache.get('ttl_test');
expect(result).toBeUndefined();
done();
}, 300);
});
});
test('should return value before TTL expires', async () => {
await cache.set('ttl_test2', 'value', 1000);
const result = await cache.get('ttl_test2');
if (cluster.isMaster) {
expect(result).toBe('value');
}
});
});
describe('Memory Management', () => {
test('should respect maxSize limit', async () => {
// Fill cache to maxSize
for (let i = 0; i < 15; i++) {
await cache.set(`key${i}`, `value${i}`);
}
// Cache size should not exceed maxSize
expect(cache.size()).toBeLessThanOrEqual(10);
});
test('should provide correct size', async () => {
await cache.set('size1', 'value1');
await cache.set('size2', 'value2');
// Size should be 2 or less (depending on timing in cluster)
expect(cache.size()).toBeGreaterThanOrEqual(0);
expect(cache.size()).toBeLessThanOrEqual(2);
});
});
describe('Utility Methods', () => {
test('should check if key exists', async () => {
await cache.set('exists_test', 'value');
// has() checks local cache only
if (cluster.isMaster) {
expect(cache.has('exists_test')).toBe(true);
}
expect(cache.has('nonexistent')).toBe(false);
});
test('should return keys array', async () => {
await cache.set('key1', 'value1');
await cache.set('key2', 'value2');
const keys = cache.keys();
expect(Array.isArray(keys)).toBe(true);
});
test('should return values array', async () => {
await cache.set('key1', 'value1');
const values = cache.values();
expect(Array.isArray(values)).toBe(true);
});
test('should return entries array', async () => {
await cache.set('key1', 'value1');
const entries = cache.entries();
expect(Array.isArray(entries)).toBe(true);
});
});
});