It’s good to use spring-data. There are mongodb and redis. It is convenient and fast to directly use spring-data-jpa to operate mongodb. Moreover, spring has a cache annotation cacheable, which can be automatically stored in the cache. spring-data address
It is recommended that redis and mongo process data separately. First get the data from mongo and then set it to redis. The mongoose-redis-cache you use seems to have poor scalability in the future, and it will be difficult to locate bugs
I don’t know much about js, so I briefly looked at the connection code you gave: 1.mongoose-redis-cache: It is equivalent to wrapping a layer of redis in front of mongo. As you said, the query statement will be cached. , key = [prefix, collectionName, hash].join ':' Use prefix, collName and query statement to generate the key in redis, first return the query result of redis; if not, check mongo; the query result of mongo is returned first set redis, set the timeout, and then call callback; 2. As the questioner said, "mongoose-redis-cache caches statements. If the database business changes, it cannot be synchronized to the cache." There is no need to worry about this. ; An available cache will definitely take into account the validity of the data, which is usually set by setting the timeout; you can also see in the code that the timeout will be set when backfilling redis.
hash = crypto.createHash('md5')
.update(JSON.stringify query)
.update(JSON.stringify options)
.update(JSON.stringify fields)
.update(JSON.stringify populate)
.digest('hex')
key = [prefix, collectionName, hash].join ':'
cb = (err, result) ->
if err then return callback err
if not result
# If the key is not found in Redis, executes Mongoose original
# exec() function and then cache the results in Redis
for k, path of populate
path.options ||= {}
_.defaults path.options, cache: false
mongoose.Query::_exec.call self, (err, docs) ->
if err then return callback err
str = JSON.stringify docs
client.setex key, expires, str
callback null, docs
else
# Key is found, yay! Return the baby!
docs = JSON.parse(result)
return callback null, docs
client.get key, cb
It’s good to use spring-data. There are mongodb and redis. It is convenient and fast to directly use spring-data-jpa to operate mongodb. Moreover, spring has a cache annotation cacheable, which can be automatically stored in the cache.
spring-data address
It is recommended that redis and mongo process data separately. First get the data from mongo and then set it to redis. The mongoose-redis-cache you use seems to have poor scalability in the future, and it will be difficult to locate bugs
I don’t know much about js, so I briefly looked at the connection code you gave:
1.mongoose-redis-cache: It is equivalent to wrapping a layer of redis in front of mongo. As you said, the query statement will be cached. ,
key = [prefix, collectionName, hash].join ':'
Use prefix, collName and query statement to generate the key in redis, first return the query result of redis; if not, check mongo; the query result of mongo is returned first set redis, set the timeout, and then call callback;
2. As the questioner said, "mongoose-redis-cache caches statements. If the database business changes, it cannot be synchronized to the cache." There is no need to worry about this. ; An available cache will definitely take into account the validity of the data, which is usually set by setting the timeout; you can also see in the code that the timeout will be set when backfilling redis.