java - mongodb配合redis缓存有没有成熟的方案?
怪我咯
怪我咯 2017-04-17 17:35:48
0
3
697

业务系统采用了Mongodb,现在想针对部分数据用redis进行缓存优化。
原来看到过一个mongoose-redis-cache:https://github.com/conancat/mongoose-red...,我理解它相当于建立一个查询语句和查询结果的键值对,但这种情况下,如果数据库业务改变后是无法同步到缓存的。想请教下各位有没有其他方案?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
巴扎黑

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!