I should be able to easily read and write values in cache-friendly data stores. I should have control over exactly when each value expires. I should be able to choose my store.
var wait = require('timer-as-promised');
var Color = app.createModel({
base: 'KeyValueModel',
keyValue: {
ttl: {
default: 120
},
namespace: 'colors'
}
});
var redis = app.createDataSource('redis', {
host: 'localhost',
port: 6379
});
Color.attachTo(redis);
var SECONDS_TO_LIVE = 60;
var key = Math.random().toString().split('.')[1];
var myValue = new Color({
key: key,
value: 'blue',
});
myValue
.setWithTTL(SECONDS_TO_LIVE)
.then(function() {
return myValue.ttl();
})
.then(function(ttl) {
console.log(ttl); // 60
return myValue.expire(120);
})
.then(function() {
return wait(120);
})
.then(function() {
return myValue.get();
})
.then(function() {
console.log(myValue.value); // => undefined
});
Color.ttl(key) and color.ttl()Color.keys() - https://github.com/strongloop/loopback/issues/2592@ritch I found few confusing parts of your example, could you please edit or clarify?
1) Where is the constructor MyCacheItem defined?
2) What's the difference between myValue.setWithTTL() and myValue.expire()?
We already have a redis connector (link). I am worried about possible confusion - when to use loopback-connector-redis and loopback-connector-kv-redis.
I made a quick research, it seams to me that SQL servers don't provide key expiration, and on the other hand, Redis does not support advanced data-access methods like "updateOrCreate", so perhaps there should be only one connector for Redis, and it should implement the key/value API only (not DataAccessObject)?
Thoughts?
Do you prefer static methods or prototype methods for KV operations? For example:
MyKVModel.set(key, val, ttl, cb) vs MyKVModel.prototype.set(ttl, cb)
Please note that we can use the model name as namespace for keys.
Connect to #2545
Please note that we can use the model name as namespace for keys.
馃憤
Do you prefer static methods or prototype methods
// I prefer this as the implementation
var kv = new KeyValue({key: 'my key', value: 'my value'});
kv.set();
// and this as the syntax sugar
KeyValue.set('my key', 'my value');
That being said we should just support both flavors.
Where is the constructor MyCacheItem defined?
Typo, edited.
What's the difference between myValue.setWithTTL() and myValue.expire()?
Atomic version of set() + expire() (expire() only sets the TTL)
I made a quick research, it seams to me that SQL servers don't provide key expiration, and on the other hand, Redis does not support advanced data-access methods like "updateOrCreate", so perhaps there should be only one connector for Redis, and it should implement the key/value API only (not DataAccessObject)?
This is exactly the reason I am suggesting we create a key-value based set of connectors. The redis connector would probably be deprecated, since it doesn't really provide enough of the DAO connector interface to be interoperable anyway.
IMO, we should provide an in-memory implementation of the KV store, similarly how we have in-memory implementation of regular DataSource. This in-memory implementation should be created as part of our work on key-value models.
cc @superkhau
This in-memory implementation should be created as part of our work on key-value models.
I agree with this because we need an in-mem connector to write tests against.
Example RI recommended by @raymondfeng: https://github.com/isaacs/node-lru-cache
@bajtos I added a new bullet list item blog post for the feature + example. Also created corresponding issue at https://github.com/strongloop-internal/scrum-loopback/issues/1015.
@bajtos @superkhau - should we deprecate the redis connector in favor of this new redis kv connector?
@ritch I'm not sure about this yet, there was one person using it recently in the PersistedModel fashion. I spoke with @raymondfeng about this before and we need a way to inject usage styles/templates to the connector at boot/runtimes to allow different usage styles for the same connector.
Closing this issue in favour of leftover individual stories: