SpringDataRedis

1、簡介

  一、SpringData和Redis

      Redis將數據存儲到內存的,速度快。能夠解決請求mysql數據庫過多而致使mysql崩潰的問題。java

      SpringData是專門用來控制Redis的工具,使用SpringData來操做Redis。mysql

      注意:在使用了Redis後,修改數據須要將Redis中的數據刪除,以後再查的時候在賦值。redis

  二、SpringDataRedis小demo

      一、導入依賴

      

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>
    </dependencies>

      二、配置文件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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath*:*.properties" />
    <!-- redis 相關配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory" />
    </bean>
</beans>

      三、配置文件redis-config.properties

redis.host=192.168.200.128
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

      四、demo

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-redis.xml"})
public class TestString {
    @Autowired
    private RedisTemplate redisTemplate;
    //放數據
    @Test
    public void testSet(){
        redisTemplate.boundValueOps("testKey").set("0708java");
    }
    //取數據
    @Test
    public void get(){
        String testKey = (String)redisTemplate.boundValueOps("testKey").get();
        System.out.println(testKey);
    }
    @Test
    public void del(){
       redisTemplate.delete("testKey");
    }
    @Test
    public void hashPut(){
        redisTemplate.boundHashOps("testHash").put("001","左青龍");
        redisTemplate.boundHashOps("testHash").put("002","右白虎");
        redisTemplate.boundHashOps("testHash").put("003","掃地僧");
        redisTemplate.boundHashOps("testHash").put("004","滅霸");
    }
    @Test
    public void hashGetOne(){
        String testHash = (String)redisTemplate.boundHashOps("testHash").get("001");
        System.out.println(testHash);
    }
    @Test
    public void hashGetAll(){
        Map<String,String> testHash = redisTemplate.boundHashOps("testHash").entries();
        Set<Map.Entry<String, String>> entries = testHash.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println("key"+entry.getKey()+" value"+entry.getValue());
        }
    }
    @Test
    public void hashDel(){
        redisTemplate.boundHashOps("testHash").delete("001");
    }
    @Test
    public void hashDelAll(){
        redisTemplate.delete("testHash");
    }
    //list
    @Test
    public void listLeftPush(){
        redisTemplate.boundListOps("001").leftPush("趙敏");
        redisTemplate.boundListOps("001").leftPush("周芷若");
        redisTemplate.boundListOps("001").leftPush("小昭");
    }
    @Test
    public void listRightPush(){
        redisTemplate.boundListOps("001").rightPush("張無忌");
    }
    @Test
    public void listGet(){
        List<String> range = redisTemplate.boundListOps("001").range(0, 10);
        for (String s : range) {
            System.out.println(s);
        }
    }
    @Test
    public void listDel(){
        redisTemplate.delete("001");
    }
}

2、分佈式項目

    在分佈式項目中。Redis就是一個緩存數據庫,速度快,不會形成mysql的數據訪問量過大。spring

  一、配置文件redis-config.properties

# Redis settings 
# server IP 
redis.host=192.168.200.128
# server port 
redis.port=6379
# server pass 
redis.pass= 
# use dbIndex 
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B 
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B  
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684  
redis.testOnBorrow=true 

  二、在service層中

    2.一、注入RedisTemplate 

    2.二、操做Redis,在查詢的時候會分爲兩種狀況,第一次先查數據庫,以後將查到的數據寫入到Redis中,以後就能夠使用Redis了。

public List<Content> findByCategoryIdFromRedis(Long categoryId) {
        //一、根據分類的id到redis中取數據
        List<Content> list=(List<Content>)redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).get(categoryId);
        //2.若是redis中沒有數據,到數據庫中取
        if(list==null){
            list=findByCategoryId(categoryId);
            //三、數據庫中獲取到數據,將數據存入redis中一份
            redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).put(categoryId,list);
        }
        return list;
    }
相關文章
相關標籤/搜索