| 98 | `; |
| 99 | |
| 100 | class SynchronizationManagerRedis implements SynchronizationManager { |
| 101 | private namespace: string; |
| 102 | private client: Redis; |
| 103 | |
| 104 | constructor() { |
| 105 | const env = useEnv(); |
| 106 | const config = getConfigFromEnv('REDIS'); |
| 107 | |
| 108 | this.client = new Redis(env['REDIS'] ?? config); |
| 109 | this.namespace = (env['SYNCHRONIZATION_NAMESPACE'] as string) ?? 'directus-sync'; |
| 110 | |
| 111 | this.client.defineCommand('setGreaterThan', { |
| 112 | numberOfKeys: 1, |
| 113 | lua: SET_GREATER_THAN_SCRIPT, |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | public async set(key: string, value: string | number): Promise<void> { |
| 118 | await this.client.set(this.getNamespacedKey(key), value); |
| 119 | } |
| 120 | |
| 121 | public get(key: string): Promise<string | null> { |
| 122 | return this.client.get(this.getNamespacedKey(key)); |
| 123 | } |
| 124 | |
| 125 | public async delete(key: string): Promise<void> { |
| 126 | await this.client.del(this.getNamespacedKey(key)); |
| 127 | } |
| 128 | |
| 129 | public async exists(key: string): Promise<boolean> { |
| 130 | const doesExist = await this.client.exists(this.getNamespacedKey(key)); |
| 131 | |
| 132 | return doesExist === 1; |
| 133 | } |
| 134 | |
| 135 | public async setGreaterThan(key: string, value: number): Promise<boolean> { |
| 136 | const client = this.client as Redis & { |
| 137 | setGreaterThan(key: string, value: number): Promise<number>; |
| 138 | }; |
| 139 | |
| 140 | const wasSet = await client.setGreaterThan(this.getNamespacedKey(key), value); |
| 141 | |
| 142 | return wasSet === 1; |
| 143 | } |
| 144 | |
| 145 | private getNamespacedKey(key: string): string { |
| 146 | return `${this.namespace}:${key}`; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | export class SynchronizedClock { |
| 151 | private key: string; |
nothing calls this directly
no outgoing calls
no test coverage detected