Write-behind、write-through 和 read-through 缓存

Redis 和其他数据库(SQL 或 NoSQL)之间的后写、直写和直读缓存。

Redis 堆栈

rghibernate 配方使用 RedisGears 函数和 Hibernate 框架来实现后写、直写和直读缓存。

这些缓存策略允许应用程序简单地连接到 Redis 缓存层,而不是底层数据库。每当应用程序更新缓存中的数据时,Redis 也会同步后端数据库中的数据。

底层数据库可以是像 MySQL 这样的 SQL 数据库,因此您需要提供一个 XML 文件,告诉 rghibernate 如何在 Redis 和其他数据库之间映射数据。

与 RGSync 的区别

RGSync

  • 基于 Python 的配方
  • 可 编程 序

rghibernate

  • 基于 Java 的配方
  • 使用 Hibernate 框架
  • 可配置而非可编程

设置后写和自读

要设置后写式缓存,请首先构建一个 rghibernate JAR 并将其注册到 RedisGears。

然后,注册以下配置文件:

  • 连接器 XML:告知 Redis 如何连接到底层数据库。

  • 映射 XML:介绍如何在两个数据库之间映射数据,例如将 Redis 哈希映射到 MySQL 表。

注册 rghibernate JAR

  1. 下载中心下载 rghibernate JAR。

  2. 将 JAR 上传到 Redis 节点。

  3. 向 RedisGears 注册 rghibernate:

    $ redis-cli -x RG.JEXECUTE com.redislabs.WriteBehind < {filepath}/rghibernate-0.1.1-jar-with-dependencies.jar
    

Configure database connection

  1. Create a connector XML file with the configuration to connect Redis to an underlying database.

  2. Upload the file to a Redis node.

  3. Register the connector configuration:

    > redis-cli -x RG.TRIGGER SYNC.REGISTERCONNECTOR mysql 1000 10 5 < src/test/resources/mysql_hibernate.cfg.xml 
    1) "OK"
    

Configure data mapping

  1. Create a mapping XML file that defines how Redis maps data to an underlying database.

  2. Upload the file to a Redis node.

  3. Register the mapping configuration for write-behind:

    > redis-cli -x RG.TRIGGER SYNC.REGISTERSOURCE StudentWrite mysql WriteBehind < src/test/resources/Student.hbm.xml 
    1) "OK"
    
  4. Register the same mapping configuration for read-through:

    > redis-cli -x RG.TRIGGER SYNC.REGISTERSOURCE StudentRead mysql ReadThrough 0 < src/test/resources/Student.hbm.xml 
    1) "OK"
    

Example configuration

Here are some example configuration files for connecting to databases and mapping data.

Connector XML

This configuration file contains connection details for an underlying MySQL database:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        
<hibernate-configuration>
  <session-factory>
    <!-- JDBC Database connection settings -->
    <property name="connection.driver_class">org.mariadb.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test?allowPublicKeyRetrieval=true&amp;useSSL=false</property>
    <property name="connection.username">user</property>
    <property name="connection.password">pass</property>
    <!-- JDBC connection pool settings ... using built-in test pool -->
    <property name="connection.pool_size">1</property>
    <!-- Echo the SQL to stdout -->
    <property name="show_sql">false</property>
    <!-- Set the current session context -->
    <property name="current_session_context_class">thread</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <!-- dbcp connection pool configuration -->
    <property name="hibernate.dbcp.initialSize">5</property>
    <property name="hibernate.dbcp.maxTotal">20</property>
    <property name="hibernate.dbcp.maxIdle">10</property>
    <property name="hibernate.dbcp.minIdle">5</property>
    <property name="hibernate.dbcp.maxWaitMillis">-1</property>
  </session-factory>
</hibernate-configuration>

Mapping XML

The following XML maps Redis hashes, which represent students, to a MySQL table:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping
    http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd">

  <class entity-name="Student" table="student">
          <tuplizer entity-mode="dynamic-map" class="org.hibernate.tuple.entity.DynamicMapEntityTuplizer"/>
          <id name="id" type="integer" length="50" column="id"/>
          <property name="firstName" column="first_name" type="string"/>
          <property name="lastName" column="last_name" type="string"/>
          <property name="email" column="email" type="string" not-null="true"/>
          <property name="age" column="age" type="integer"/>
  </class>

</hibernate-mapping>

Commands

Run rghibernate commands with RG.TRIGGER:

redis-cli RG.TRIGGER SYNC.<COMMAND>

To pass a file to a command like SYNC.REGISTERCONNECTOR or SYNC.REGISTERSOURCE, use the redis-cli -x option:

redis-cli -x RG.TRIGGER SYNC.{COMMAND} {arg1 arg2 ...} < {file}

Command list

Command Description
SYNC.REGISTERCONNECTOR Register a new connector
SYNC.UNREGISTERCONNECTOR Unregister a connector (cannot have sources attached)
SYNC.REGISTERSOURCE Extra configuration based on policy
SYNC.UNREGISTERSOURCE Unregister a source
SYNC.INFO SOURCES Dump all sources
SYNC.INFO CONNECTORS Dump all connectors

SYNC.REGISTERCONNECTOR

$ redis-cli -x RG.TRIGGER SYNC.REGISTERCONNECTOR \
    {connector name} {batch size} {timeout} {retry interval} \
    < {connector xml}
Name Description
connector name Name to give to your connector
batch size The size of the data sent to the backend in batches
timeout After this timeout, sends data to the backend even if the batch size was not reached
retry interval Retry interval on error
connector xml Hibernate XML definition of the connector

SYNC.REGISTERSOURCE

redis-cli -x RG.TRIGGER SYNC.REGISTERSOURCE \
    {source name} {connector name} {policy} \
    < {mapping xml}
Name Description
source name Name to give to your source
connector name Connector to send the data to
policy WriteBehind/WriteThrough/ReadThrough:

• On WriteThrough, an extra argument is WriteTimeout

• On ReadThrough, an extra argument is expire (0 for no expire)
mapping xml Hibernate XML definition of the mapping
RATE THIS PAGE
Back to top ↑