mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
Refactor to guard
This commit is contained in:
1 parent
92383486dc
commit
c3bcc75e88
1 file changed
+29
-28
+29
-28
@@ -26,35 +26,36 @@ export class CacheHelper {
|
||||
): Promise<T | undefined> {
|
||||
let cache = await this.getData<T>(key);
|
||||
|
||||
// Nothing in the cache, acquire a lock to prevent multiple writes
|
||||
if (!cache) {
|
||||
let lock;
|
||||
const lockKey = `lock:${key}`;
|
||||
try {
|
||||
try {
|
||||
lock = await MutexLock.lock.acquire(
|
||||
[lockKey],
|
||||
MutexLock.defaultLockTimeout
|
||||
);
|
||||
} catch (err) {
|
||||
Logger.error(`Could not acquire lock for ${key}`, err);
|
||||
}
|
||||
cache = await this.getData<T>(key);
|
||||
if (cache) {
|
||||
return cache;
|
||||
}
|
||||
|
||||
// Get the data from the callback and save it in cache
|
||||
const value = await callback();
|
||||
if (value) {
|
||||
await this.setData<T>(key, value, expiry);
|
||||
}
|
||||
return value;
|
||||
} finally {
|
||||
await lock?.release();
|
||||
}
|
||||
if (cache) {
|
||||
return cache;
|
||||
}
|
||||
|
||||
// Nothing in the cache, acquire a lock to prevent multiple writes
|
||||
let lock;
|
||||
const lockKey = `lock:${key}`;
|
||||
try {
|
||||
try {
|
||||
lock = await MutexLock.lock.acquire(
|
||||
[lockKey],
|
||||
MutexLock.defaultLockTimeout
|
||||
);
|
||||
} catch (err) {
|
||||
Logger.error(`Could not acquire lock for ${key}`, err);
|
||||
}
|
||||
cache = await this.getData<T>(key);
|
||||
if (cache) {
|
||||
return cache;
|
||||
}
|
||||
|
||||
// Get the data from the callback and save it in cache
|
||||
const value = await callback();
|
||||
if (value) {
|
||||
await this.setData<T>(key, value, expiry);
|
||||
}
|
||||
return value;
|
||||
} finally {
|
||||
await lock?.release();
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user