【Redis】Jedis使用

单机版使用

1.添加pom

<!-- Redis客户端 -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

2.Jedis Demo

//创建一个连接Jedis对象,参数:host、port
Jedis jedis = new Jedis("192.168.21.80", 6379);
//直接使用jedis操作redis。所有jedis的命令都对应一个方法。
jedis.set("123", "my first jedis test");
String string = jedis.get("123");
System.out.println(string);
//关闭连接
jedis.close();

3.JedisPool Demo

//创建一个连接池对象,两个参数host、port
JedisPool jedisPool = new JedisPool("192.168.25.162", 6379);
//从连接池获得一个连接,就是一个jedis对象。
Jedis jedis = jedisPool.getResource();
//使用jedis操作redis
String string = jedis.get("test123");
System.out.println(string);
//关闭连接,每次使用完毕后关闭连接。连接池回收资源。
jedis.close();
//关闭连接池。
jedisPool.close();

4.修改Spring配置文件

<!-- 连接redis单机版 -->
<bean id="jedisClientPool" class="cn.e3mall.common.jedis.JedisClientPool">
    <property name="jedisPool" ref="jedisPool"></property>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="host" value="192.168.21.80"/>
    <constructor-arg name="port" value="6379"/>
</bean>

5.使用JedisClient

//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
//从容器中获得JedisClient对象
JedisClient jedisClient = applicationContext.getBean(JedisClient.class);
jedisClient.set("test", "jedisClient");
String string = jedisClient.get("test");
System.out.println(string);

集群版使用

1.JedisCluster Demo

//创建一个JedisCluster对象。有一个参数nodes是一个set类型。set中包含若干个HostAndPort对象。
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.21.80", 7001));
nodes.add(new HostAndPort("192.168.21.80", 7002));
nodes.add(new HostAndPort("192.168.21.80", 7003));
nodes.add(new HostAndPort("192.168.25.162", 7004));
nodes.add(new HostAndPort("192.168.21.80", 7005));
nodes.add(new HostAndPort("192.168.21.80", 7006));
JedisCluster jedisCluster = new JedisCluster(nodes);
//直接使用JedisCluster对象操作redis。
jedisCluster.set("test", "123");
String string = jedisCluster.get("test");
System.out.println(string);
//关闭JedisCluster对象
jedisCluster.close();

2.添加Spring配置文件

<!-- 连接redis集群 -->
<bean id="jedisClientCluster" class="cn.e3mall.common.jedis.JedisClientCluster">
    <property name="jedisCluster" ref="jedisCluster"/>
</bean>
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
    <constructor-arg name="nodes">
        <set>
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.21.80"></constructor-arg>
                <constructor-arg name="port" value="7001"></constructor-arg>
            </bean> 
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.21.80"></constructor-arg>
                <constructor-arg name="port" value="7002"></constructor-arg>
            </bean> 
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.21.80"></constructor-arg>
                <constructor-arg name="port" value="7003"></constructor-arg>
            </bean> 
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.21.80"></constructor-arg>
                <constructor-arg name="port" value="7004"></constructor-arg>
            </bean> 
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.21.80"></constructor-arg>
                <constructor-arg name="port" value="7005"></constructor-arg>
            </bean> 
            <bean class="redis.clients.jedis.HostAndPort">
                <constructor-arg name="host" value="192.168.21.80"></constructor-arg>
                <constructor-arg name="port" value="7006"></constructor-arg>
            </bean> 
        </set>
    </constructor-arg>
</bean>

3.使用JedisClient,同单机版

//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
//从容器中获得JedisClient对象
JedisClient jedisClient = applicationContext.getBean(JedisClient.class);
jedisClient.set("test", "jedisClient");
String string = jedisClient.get("test");
System.out.println(string);

注意事项

  • 单机和集群的切换通过修改Spring配置文件实现;
  • 在原业务中插入redis,要放在try catch中,不影响原业务;
  • 缓存内的数据对应的数据库数据被修改后,要删掉或修改redis中的数据;

猜你喜欢

转载自blog.csdn.net/Francis123580/article/details/81639130