Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions axios-cache-adapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export declare function setup(options: AxiosRequestConfig): AxiosInstance;
export declare function setupCache(options: IAxiosCacheAdapterOptions) : ISetupCache;

export class RedisStore { constructor(client: any, HASH_KEY?: string); }
export class IoRedisStore { constructor(client: any, HASH_KEY?: string); }

export interface IAxiosCacheAdapterRequest
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"codecov": "^3.0.0",
"html-webpack-plugin": "^3.0.6",
"husky": "^1.1.2",
"ioredis-mock": "^4.18.4",
"istanbul-instrumenter-loader": "^3.0.0",
"karma": "^2.0.0",
"karma-chrome-launcher": "^3.1.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setup, setupCache, serializeQuery } from './api'
import RedisStore from './redis'
import IoRedisStore from './ioredis'

export { setup, setupCache, serializeQuery, RedisStore }
export default { setup, setupCache, serializeQuery, RedisStore }
export { setup, setupCache, serializeQuery, RedisStore, IoRedisStore }
export default { setup, setupCache, serializeQuery, RedisStore, IoRedisStore }
38 changes: 38 additions & 0 deletions src/ioredis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { mapObject } from './utilities'

class IoRedisStore {
constructor (client, HASH_KEY = 'axios-cache') {
this.client = client
this.HASH_KEY = HASH_KEY
}

async getItem (key) {
const item = (await this.client.hget(this.HASH_KEY, key)) || null

return JSON.parse(item)
}

async setItem (key, value) {
await this.client.hset(this.HASH_KEY, key, JSON.stringify(value))
return value
}

async removeItem (key) {
await this.client.hdel(this.HASH_KEY, key)
}

async clear () {
await this.client.del(this.HASH_KEY)
}

async length () {
return this.client.hlen(this.HASH_KEY)
}

async iterate (fn) {
const hashData = await this.client.hgetall(this.HASH_KEY)
return Promise.all(mapObject(hashData, fn))
}
}

export default IoRedisStore
74 changes: 74 additions & 0 deletions test/spec/ioredis.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* globals describe it beforeEach */

import assert from 'assert'
import Redis from 'ioredis-mock'
import RedisStore from 'src/ioredis'

describe('Redis store', () => {
let store
const client = new Redis()

beforeEach(() => {
store = new RedisStore(client)
})

it('Should accept custom HASH_KEY', async () => {
const expected = 'customHash'
store = new RedisStore(client, expected)
assert.equal(store.HASH_KEY, expected)
})

it('getItem(): Should retrieve an item', async () => {
const expected = 'bar'

await store.setItem('foo', expected)

const value = await store.getItem('foo')

assert.equal(value, expected)
})

it('setItem(): Should set an item', async () => {
const expected = 'bar'

await store.setItem('foo', expected)

const value = await store.getItem('foo')

assert.equal(value, expected)
})

it('removeItem(): Should remove an item', async () => {
await store.setItem('foo', 'bar')

await store.removeItem('foo')

const value = await store.getItem('foo')
assert.equal(value, null)
})

it('clear(): Should clear all set values', async () => {
await store.setItem('foo', 'bar')
await store.setItem('hello', 'hello')

await store.clear()

const length = await store.length()

assert.equal(length, 0)
})

it('Should serialize stored data to prevent modification by reference', async () => {
const data = {
key: 'value'
}

await store.setItem('key', data)

data.key = 'other value'

const storedData = await store.getItem('key')

assert.notEqual(data.key, storedData.key)
})
})