Skip to content

Commit

Permalink
Merge branch 'release/0.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Feb 10, 2024
2 parents 89bb818 + 30d8ff2 commit 7a34dd8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
35 changes: 17 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ module.exports = function defineSailsCacheHook(sails) {
return {
defaults: {
stash: {
store: process.env.CACHE_STORE || 'redis',
stores: {
redis: {
store: 'redis',
datastore: 'cache',
},
memcached: {
store: 'memcached',
datastore: 'cache',
},
cachestore: 'default',
},
cachestores: {
default: {
store: 'redis',
datastore: 'cache',
},
},
},
initialize: async function () {
function getCacheStore(store) {
switch (sails.config.stash.stores[store].store) {
function getCacheStore(cachestore) {
if (!sails.config.cachestores[cachestore]) {
throw new Error('The provided cachestore coult not be found.')
}
switch (sails.config.cachestores[cachestore].store) {
case 'redis':
return new RedisStore(sails)
default:
throw new Error('Invalid cache store provided')
throw new Error(
'Invalid store provided, supported stores are redis or memcached.',
)
}
}

let cacheStore = getCacheStore(
sails.config.stash.stores[sails.config.stash.store].store,
)
let cacheStore = getCacheStore(sails.config.stash.cachestore)

sails.cache = {
get: cacheStore.get.bind(cacheStore),
Expand All @@ -41,8 +40,8 @@ module.exports = function defineSailsCacheHook(sails) {
pull: cacheStore.pull.bind(cacheStore),
forever: cacheStore.forever.bind(cacheStore),
destroy: cacheStore.destroy.bind(cacheStore),
store: function (store) {
return getCacheStore(store)
store: function (cachestore) {
return getCacheStore(cachestore)
},
}
},
Expand Down
3 changes: 2 additions & 1 deletion lib/stores/cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function CacheStore(sails) {
}

this.sails = sails
this.datastore = sails.config.stash.stores[sails.config.stash.store].datastore
this.datastore =
sails.config.cachestores[sails.config.stash.cachestore].datastore
this.store = null
}
/**
Expand Down
1 change: 0 additions & 1 deletion lib/stores/redis-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function RedisStore(sails) {

RedisStore.prototype = Object.create(CacheStore.prototype)
RedisStore.prototype.constructor = RedisStore
RedisStore.prototype.getStore = CacheStore.prototype.getStore

/**
* Retrieves the value associated with the specified key from the cache store.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-stash",
"version": "0.0.1",
"version": "0.0.2",
"description": "Efficient and intuitive caching for your Sails applications.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 7a34dd8

Please sign in to comment.