ut-port-redis
ut-port-redis
is a redis object caching port.
It is a convinient wrapper for ionredis
and provides on top of that the standard ut
mechanisms for logging,
monitoring, error handling, etc. inherited from
ut-port.
Usage
This port can be used the same way as any other ut
compliant port.
For more information about how to bootstrap a ut port
click here
Configuration
Besides logLevel
, concurrency
, and other configuration options
which are common for all ports, ut-port-redis defines 2 additional
properties:
-
client
- This object provides a low-level cache abstraction.-
options
- The strategy configuration object
-
More information about how to configure the policy options
can be found here
Configuration example
function cache() {
return class cache extends require('ut-port-redis')(...arguments) {
get defaults() {
options: {} // ionredis-redis options
}
};
}
API
By setting up the port using the example above it will expose the following set of methods available through ut-bus:
-
bus.importMethod('cache/module.entity.action')(msg, $meta)
-
module.entity.action
is arbitrary string, usually corresponding to a namespaced method call -
msg
is the value for the cache operation and has the following structure {operation
,key
,value
}-
operation
is a valid redis command -
key
is a string identifier used to locate/create a key value pair -
value
is optional depending on the redis operation
-
-
$meta
contains the metadata associated with the request -
return
value is determined by the redis operation
-
Example
This is a trivial example illustrating a set and get operation
const msg = {
key : 'foo',
operation: 'set',
value: 'bar'
}
Promise.resolve()
.then(result => {
return bus.importMethod('cache/module.foo.set')(msg, $meta)
// returns promise which resolves to string, "OK"
.then(() => {
return bus.importMethod('cache/module.foo.get')({
key : 'foo',
operation: 'get'
}, $meta) // Promise resolves to "bar"
});
})
.catch(e => { // an exception in case any of the above calls fail
throw e;
});