search

Home  >  Q&A  >  body text

redis中如何保持关系数据库里的一行记录

要将关系数据库中表的一行记录保持到redis中,在redis里要采用哪种数据类型,怎么保存?希望能举个具体示例

高洛峰高洛峰2796 days ago627

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-24 09:12:58

    Serialize java objects and store them as byte arrays

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:12:58

    You can directly use the primary key of the database as redis, such as <YOUR_PREFIX><PRIMARY_KEY>. For content, for example, you can directly record json in the data table

    Then there’s the issue of consistency

    • The first solution is to use redis as a cache. When reading records according to the primary key, first check whether there is one in redis (get). If not, get it from the database and set the corresponding value in redis. , it is best to set an expire time for the record when setting, so that even if the dirty data is not actively cleared, the new value can be automatically obtained through the expire mechanism after a period of time. When the data table records change, just delete the records in redis directly. This is a safer approach that I recommend.

    • The second option is to handle all data reading and writing directly in redis, and only write the data back to the database at specific times. Regarding the writeback mechanism, you can consider using a message queue to write data back asynchronously every time redis is modified. You can use a timestamp to ensure that the data written back to the database is the latest; or you can consider using Redis’ notification mechanism for expire timing (but this mechanism is not guaranteed, see http://redis.io/topics/notifications).

    reply
    0
  • 怪我咯

    怪我咯2017-04-24 09:12:58

    This is quite troublesome
    You can refer to this article
    http://www.cnblogs.com/enjiex/p/3618546.html

    It can support simple additions, deletions, modifications and queries, and can be combined with redis transactions to implement simple functions
    But it is very troublesome to implement the basic SQL operations of rdbms

    In summary

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:12:58

    Read the manual obediently. The answers others give you will not be useful. The manual explains it very clearly. After reading the manual, consider the application scenarios. When it comes to the practical stage, there are some encapsulated third-party APIs that can be called. In fact, you The same goes for writing it yourself. Start writing from sockets and you will have a new understanding of protocols, languages, and networks

    reply
    0
  • Cancelreply