Skip to content

Commit

Permalink
fix(cache): fix default cache options resolution (#43)
Browse files Browse the repository at this point in the history
* fix(cache): fix default cache options resolution

* fix(cache): fix lru cache not properly setting value

* fix(cache): default type fix
  • Loading branch information
bartolkaruza committed Dec 19, 2019
1 parent de71542 commit 88961bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
63 changes: 31 additions & 32 deletions lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,34 @@ export const defaultCacheOptions: TileCacheOptions = {
redisOptions: {
ttl: 86400, // 24 hours
host: process.env.REDIS_HOST,
dropBufferSupport: false,
},
};

export function Cache(options: TileCacheOptions = defaultCacheOptions) {

export function Cache(
customCacheOptions: TileCacheOptions = defaultCacheOptions
) {
let lruCache = null;
let redisCache = null;
const cacheOptions = {
...defaultCacheOptions,
...customCacheOptions,
lruOptions: {
...defaultCacheOptions.lruOptions,
...customCacheOptions.lruOptions,
},
redisOptions: {
...defaultCacheOptions.redisOptions,
...customCacheOptions.redisOptions,
dropBufferSupport: false,
},
};

if (options.type === 'lru-cache') {
if (cacheOptions.type === 'lru-cache') {
const LRU = require('lru-cache');
lruCache = new LRU({
...defaultCacheOptions.lruOptions,
...options.lruOptions,
});
} else if (options.type === 'redis') {
lruCache = new LRU(cacheOptions.lruOptions);
} else if (cacheOptions.type === 'redis') {
const Redis = require('ioredis');

redisCache = new Redis({
...defaultCacheOptions.redisOptions,
...options.redisOptions,
...{ dropBufferSupport: false },
});
redisCache = new Redis(cacheOptions.redisOptions);
}

return {
Expand All @@ -61,7 +66,7 @@ export function Cache(options: TileCacheOptions = defaultCacheOptions) {
y: number,
filters: string[]
): string => {
if (!options.enabled || !options.enable) {
if (!cacheOptions.enabled || !cacheOptions.enable) {
return null;
}
const where = sha1(
Expand All @@ -79,15 +84,15 @@ export function Cache(options: TileCacheOptions = defaultCacheOptions) {
* @returns The cache value or null if not found or disabled
*/
getCacheValue: async (key: string): Promise<any> => {
if (!options.enabled || !options.enable) {
if (!cacheOptions.enabled || !cacheOptions.enable) {
return null;
}

if (options.type === 'lru-cache') {
if (cacheOptions.type === 'lru-cache') {
return lruCache.get(key);
}

if (options.type === 'redis') {
if (cacheOptions.type === 'redis') {
return await redisCache.getBuffer(key);
}

Expand All @@ -102,24 +107,18 @@ export function Cache(options: TileCacheOptions = defaultCacheOptions) {
* @param {TileCacheOptions} options The cache options
*/
setCacheValue: async (key: string, value: any): Promise<void> => {
if (!options.enabled || !options.enable) {
if (!cacheOptions.enabled || !cacheOptions.enable) {
return null;
}

if (options.type === 'lru-cache') {
lruCache.set(key);

return;
}

if (options.type === 'redis') {
if (!!options.redisOptions.ttl) {
await redisCache.set(key, value, 'EX', options.redisOptions.ttl);
if (cacheOptions.type === 'lru-cache') {
lruCache.set(key, value);
} else if (cacheOptions.type === 'redis') {
if (!!cacheOptions.redisOptions.ttl) {
await redisCache.set(key, value, 'EX', cacheOptions.redisOptions.ttl);
} else {
await await redisCache.set(key, value);
await redisCache.set(key, value);
}

return;
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion types/TileCacheOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface TileCacheOptions {
/**
* @deprecated replaced by {enabled}
*/
enable?: boolean
enable?: boolean;

/**
* @description The type of the cache. Default is lru-cache
Expand Down

0 comments on commit 88961bb

Please sign in to comment.