### Install ioredfour Source: https://github.com/nodemailer/ioredfour/blob/master/README.md Install the ioredfour package using npm. This is the first step before using the library. ```sh npm install ioredfour ``` -------------------------------- ### Basic Lock Usage with ioredfour Source: https://github.com/nodemailer/ioredfour/blob/master/README.md Demonstrates acquiring a lock, handling existing locks, and waiting for a lock to be released. Ensure the lock TTL is at least 1.5x the replicationTimeout. ```js const Lock = require('ioredfour'); (async () => { const testLock = new Lock({ // Can also be an `Object` of options to pass to `new Redis()` // https://www.npmjs.com/package/ioredis#connect-to-redis, or an existing // instance of `ioredis` (if you want to reuse one connection, though this // module must create a second). redis: 'redis://localhost:6379', namespace: 'mylock', // Don't consider the lock owned until writes have been replicated at least this many times minReplications: 1, // Wait at most this many milliseconds for replication. // Note: lock TTL must be at least 1.5x replicationTimeout. replicationTimeout: 500 }); const id = Math.random(); // First, acquire the lock. const firstlock = await testLock.acquireLock(id, 60 * 1000 /* Lock expires after 60sec if not released */).catch(e => { console.log('error acquiring first lock', e); }); if (!firstlock.success) { console.log('lock exists', firstlock); } else { console.log('lock acquired initially'); } // Another server might be waiting for the lock like this. testLock .waitAcquireLock(id, 60 * 1000 /* Lock expires after 60sec */, 10 * 1000 /* Wait for lock for up to 10sec */) .then(secondlock => { if (secondlock.success) { console.log('second lock acquired after wait!', secondlock); } else { console.log('second lock not acquired after wait!', secondlock); } }) .catch(e => { console.log('error wait acquiring', e); }); // When the original lock is released, `waitAcquireLock` is fired on the other server. setTimeout(async () => { try { await testLock.releaseLock(firstlock); console.log('released lock'); } catch (e) { console.log('error releasing', e); } }, 10 * 1000); })(); ``` -------------------------------- ### Use Existing ioredis Cluster Connection Source: https://github.com/nodemailer/ioredfour/blob/master/README.md Initialize ioredfour with an existing ioredis Cluster instance. This allows reusing an established connection. ```js const Redis = require('ioredis'); const Lock = require('ioredfour'); const cluster = new Redis.Cluster([ { host: '127.0.0.1', port: 7000 }, { host: '127.0.0.1', port: 7001 }, { host: '127.0.0.1', port: 7002 } ]); const lock = new Lock({ // `redisConnection` is also supported as an alias: // redisConnection: cluster, redis: cluster, namespace: 'mylock' }); ``` -------------------------------- ### Configure ioredfour for Redis Cluster Source: https://github.com/nodemailer/ioredfour/blob/master/README.md Initialize ioredfour to use a Redis Cluster. This configuration requires specifying cluster nodes and optionally ioredis cluster options. ```js const Lock = require('ioredfour'); const lock = new Lock({ namespace: 'mylock', cluster: [ { host: '127.0.0.1', port: 7000 }, { host: '127.0.0.1', port: 7001 } ], // Optional ioredis Cluster options: // https://github.com/redis/ioredis#cluster clusterOptions: { redisOptions: { password: process.env.REDIS_PASSWORD } } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.