search

Home  >  Q&A  >  body text

java - redis 缓存方案--有没有可用的?

现在的方案是,sql 作为key 查询结果序列化后 存入value 。表如果有数据修改添加删除,flushDB()。
但是整个项目只要有数据变更缓存就没了。
谁还有其他方案?
想知道的就是数据增删改对redis都有操作后再对数据库操作,redis里查询的永远是最新的,当某些原因redis里查不到时查数据库然后保存到reids里。
这种方案有谁实现了吗?

黄舟黄舟2766 days ago476

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-04-18 09:15:22

    Why do you need to use sql as the key... Every time you flush db, you can change your mind. If there is a table with products and the primary key is id, you can create a cache with the key detail: product id and record this line As value, the data type hash or string is acceptable. Sometimes when your business needs to check a certain product information based on the primary key, you first go to redis to see if there is a cache of detail: primary key. Sometimes, you just fetch the cache directly. If there is no hit in the cache, you check the database and save the query results. cache. Also, when the record changes, you can directly delete the cache or update the cache.
    This is just a very simple example. Redis data types are very rich and can be used very happily if used properly. Also, in the above example, you can set a validity period for each cache, such as one week, it depends on your business. Generally speaking, users rarely view old content, so after it expires and no one browses it, it will not be rebuilt. This ensures that your redis always has hot data. It’s not just a bunch of old data. I’m tired of typing on my phone. Please like it if it’s useful

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:15:22

    The meaning of caching is to store popular data. For example, storing data that is frequently used by the database in redis, so that you don’t have to query the database every time.

    The problem encountered by the poster is how to keep the data in the cache consistent with the data in the database.
    As mentioned above表如果有数据修改添加删除,flushDB(), it is not advisable.

    The operation of cached data is actually simplified to read and write

    When your application queries redis, if it cannot be found, it needs to check the database at this time and save the found data in redis.
    When the data in the database is changed, the above mentioned 数据增删改对redis都有操作后再对数据库操作 is actually not advisable. The application should modify the database first, and then update redis after success. What should I do if I update redis first, but the update to the database fails? Doesn't redis store dirty data?

    In addition, the data in the cache can set an expiration time (expire timeout). After the expiration time, check the database again and update the data (even if the data has not changed at all). Before the expiration time, at least the database will be reduced. load.

    Looking at the purpose of the theme, there is a way to think of redis as a backup of database data. That would not be the point of using redis cache (perhaps you should consider database master-slave backup).

    As for the solution you mentioned, looking at the description of the topic, it does not require a solution (I think high-availability configuration, considering the consistency of the data, etc., is called a solution).
    It’s actually the logic of the code, it depends on how the code is written.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:15:22

    Spring has a redis plug-in. It is more convenient to use ehcache and spring plug-ins for small projects.

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:15:22

    When you implement a caching system, you must consider these three questions (the third question is optional)

    1. Cached loading

    2. Cache invalidation

    3. Cached updates (optional)

    Question 1:

    To load the cache, what you have to do is very simple. For any query, first query the cache. If there is no hit in the cache, then check the database.

    Question 2:

    To invalidate the cache, all you have to do is remember to delete the cache when any modification (update, delete) is made to the database.

    Question 3:

    When the data is updated, the cache will not be invalidated and deleted, but the cache will be updated. This optional operation can help you achieve the effect that the cache is always the latest.

    In response to question 3, there is another problem. Do not update the cache before the database has been updated successfully. The database update may fail. At this time, if your cache is not restored, dirty data will appear.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:15:22

    mybatis-redis has a solution, you can refer to https://github.com/mybatis/redis-cache

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:15:22

    Implemented @Cacheable,使用在servicedao层的方法上,用来进行拦截,直接去redisretrieving data.

    reply
    0
  • Cancelreply