Redis学习

红包,优惠券过期通知:

安装:直接解压

redis启动

方式一(没有进行redis一些配置):

直双击C:\Users\Administrator\Downloads\Redis-x64-3.2.100\redis-server.exe文件

方式二

打开cmd命令行,输入cd C:\Users\Administrator\Downloads\Redis-3.2.100-Windows-32\Redis,再输入命令:redis-server.exe redis.windows.conf

测试redis是否启动成功:双击C:\Users\Administrator\Downloads\Redis-3.2.100-Windows-32\Redis\redis-cli.exe文件

注意:需要将启动redis窗口打开,然后在直接双击redis-cli.exe文件才能看到如下:

首先打开一个redis客户端,然后订阅一个频道ITCAST,等待接收消息....

 

打开另外一个客户端,然后发布一个主题消息,ITCAST “very good”

 

subscribe __keyevent@0__:expired

 

Spring整合spring data redis开发:

 

applicationContext-redis.xml配置

------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/context/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 整合springDataRedis 核心API:redisTemplate,用来操作redis服务器的 -->

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <!-- 设置key的序列化方式 -->
        <property name="keySerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <!-- 设置value的序列化方式 -->
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <!-- 注入连接工厂 -->
        <property name="connectionFactory" ref="connectionFactory"></property>
    </bean>

    <!-- 创建connectionFactory交给spring容器来管理 -->
    <bean id="connectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1"></property>
        <property name="port" value="6379"></property>
        <property name="database" value="0"></property>        <!-- redis默认有16个数据库 -->
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>

    <!-- 创建连接池的基本配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="10"></property>
    </bean>

</beans>   

=====================================================================================

RedisTest测试代码

--------------------------------------------------------------------------------------------

package cn.itcast.redis.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 启动该类的时候请先手动启动Redis服务器,执行成功之后,打开redis客户端输入get "itcast001",结果为"itcast001"
 * 否则将报错:org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested
 * @author Administrator
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml")
public class RedisTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void testRedis() {
        redisTemplate.opsForValue().set("itcast001", "itcast001");
    }

}
================================================================================

测试结果:

 

 

 

 

 

posted @ 2019-05-08 15:53  愿世界对你温柔相待  Views(56)  Comments(0)    收藏  举报