I want to use mongo to store a global feed's data but I want to cache the most recent posts in memory with redis. Is there a way to set up one model to use two adapters? If there isn't I think I'm going to try to have one controller use two models.
It's an experimental feature, but you can have a single model use multiple connections, and the connections can use different adapters:
In a Sails v0.9.x model, with mongo
and redis
defined in /config/adapters.js
:
module.exports = {
adapter: ['mongo', 'redis']
attributes: {...}
}
In a Sails v0.10.x model, with mongo
and redis
defined in /config/connections.js
:
module.exports = {
connections: ['mongo', 'redis']
attributes: {...}
}
However, you can't specify that some attributes use one connection, and other attributes use another. If more than one adapter is involved, and the adapters have identical methods, then they will be merged together. This would be the case for using sails-mongo
and sails-redis
in the same model; both adapters have find
, create
, update
and destroy
, so your model would end up using only one of the adapters' methods (sails-redis
, in the example above).
What's the use of multiple adapters then? It's a way to add external API methods to a model. For example, if you had a File
model representing a file living on Amazon S3, you could use a sails-s3
adapter with upload
and download
methods, alongside sails-mongo
to store metadata about the file.
Caching is also a feature which is in active development, but in the meantime your best bet would be to use a separate model for the cache, as you suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With