Memset is a persistent, in memory, self updating cache tool. The main difference between Memset and Cache is that Cache sets and returns data from redis using specified triggers within your code, whereas Memset provides self updating cached data from memory which is accessible synchronously.
You should consider using Memset when:
- You have persistent data that doesn't need updating on demand
- You always need certain data available quickly and is used frequently
Firstly, ensure you have RediBox installed.
Install Memset via npm:
npm install redibox-hook-memset --save
Within your redibox
config, we'll setup a new memset
object containing a sets
array. Each set item consists of a key
name, runs
function and interval
.
- key: A property name in which the data will be stored under and accessed within Memset.
- runs: A function or string (which returns a function) of the data to store. This must return a promise or data.
- interval: A string of the update time, compatible with (Later.js)(https://bunkat.github.io/later/parsers.html#text). Internally this use a similar setup to Schedule.
{
memset: {
sets: [{
key: 'categories',
runs: function(set, sets) {
// Return a promise or data
return ProductCategories.find({ active: true });
},
interval: 'every 5 minutes',
}]
}
}
When your application boots - all of the sets are run, no matter what the interval
. This means your data is accessible at all times.
Very simply access the data by key name:
// assumes 'RediBox' is your predefined redibox instance
const categories = RediBox.hooks.memset.categories;
// With Sails hook
const categories = Memset.categories;
If your sets require other Memset data to run, keep in mind that on boot the sets run in the order provided in the array. For example:
Broken Example:
sets: [
{
key: 'cars',
runs: function(set, sets) {
// Return a promise or data
return Cars.find({
manufacturer: sets.carManufacturers,
});
},
interval: 'every 2 minutes',
}, {
key: 'carManufacturers',
runs: function(set, sets) {
// Return a promise or data
return CarManufacturers.find({ active: true });
},
interval: 'every 5 minutes',
}
]
This won't work because on boot, the carManufacturers
data hasn't been set. To fix this, the carManufacturers
would need to come before cars
.
Working example:
sets: [
{
key: 'carManufacturers',
runs: function(set, sets) {
// Return a promise or data
return CarManufacturers.find({ active: true });
},
interval: 'every 5 minutes',
}, {
key: 'cars',
runs: function(set, sets) {
// Return a promise or data
return Cars.find({
manufacturer: sets.carManufacturers,
});
},
interval: 'every 2 minutes',
},
]
This will now function as expected because 'Cars' can now access to the data previously created by CarManufacturers.
Before diving into Memset, you should first understand when to use Memset over the Cache methods.
Memset should be used for common top level datasets which are accessed across your application, which is not likely to frequently update. Cache should be used for low level specific datasets which are less likely to be accessed and frequently need updating.
Example: Imagine an online shopping website, where users are able to browse for products via category, view a single product and login to our website. The website contains around 100 categories and thousands of products.
Memset:
- The product categories would be commonly accessed dataset on the website, which do not change frequently. In this case we could use Memset to update the categories every 5 minutes, which would be reflected across the site to all users.
Cache:
- Since a single product is unlikely to be accessed frequently, and could be subject to regularly fluctuating prices changes/stock changes it would not make sense to store this in Memset. Instead Cache should be used for specific product control with low cache times or the cached product data will need to be reset on price change within the application (
Cache.del()
). - The amount of users in the application is subject to constant change, along with users data/settings/authentication. This would not make sense to store in Memset, and should be controlled individually with custom Cache levels.