Skip to content

Instantly share code, notes, and snippets.

@tcharlat
Created January 28, 2016 22:56
Show Gist options
  • Save tcharlat/bc4fed44462e9410975c to your computer and use it in GitHub Desktop.
Save tcharlat/bc4fed44462e9410975c to your computer and use it in GitHub Desktop.
Proposition :
1
the key used to write or read is currently waterline:<collectionName>:<primaryKeyName>:<primaryKeyValue>
It would be nice to allow the user to define its own prefix.
How : (one or several of the following)
- defaults to waterline:<collectionName>:<primaryKeyName>: (current)
- if prefix is set in the model at the class level and is a function, expect it to
output a prefix string once, replaces the default at each collection instantiation
- if prefix is set in the model at the class level and is a string, replaces the default
- if prefix is set in the model at the instance level and is a function() execute this function at each 'create'
or 'find' event
2
sails-redis creates tables to manage uniqueness. datas stored in these table lead to sails-redis
throwing error if we try to create.
Problems :
- errors are not iso with other adapters error and leads to heterogeneous outputs if error is served through the api
- if we set ttl by hand, a key might be deleted by REDIS but is still in the uniqueness-checking table
-> we can't use a primaryKey other than autoincrementing int
-> or we have to destroy() before create() just to clean the unique table (might be throwing or not acting on the table
if destroy() doesn't find the key)
-> if we avoid custom primaryKey, we still have a growing unique table
Possible fix
- introduce a dontManageUniquenessForThisModel flag, which deactivates most of the complicated code I have trouble getting through
(800+ lines files are hard to get for a beginner)
-> I would do that in my own fork if I understood how this whole uniqueness foolproof system is working and
could comment out some functions without fear of inducing errors or instability. I actually tried a little bit,
but there is so much logic and code it's hard to keep track.
- create a setTTL() method that removes the unique entry from the unique table, or somehow sets an expire there as well !
3 sails-redis could expose dedicated set() / get() semantics
Problem : we can use require('redis') but we lose our model standardization, lifecycle callbacks, type checking, etc.
I would like to have the following functions : (instance methods)
set() {
var key = [this._model.prefix, this[this._model.primaryKey]].join('');
var value = this.toObj;
delete value[this.primaryKey];
redis.hmset(key, JSON.stringify(value));
if (this._model.expireDuration)
redis.expires(key, this._model.expireDuration);
}
Why set and not create ? Create is a waterline semantic item that checks things like uniqueness.
If create() would not check uniqueness and do expire if an expire option is set, that would be perfect
setTTL() // really simple
This method might also setttl to the indexed set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment