品优购项目笔记(三):SpringDataRedis

angularJS页面之间传参

一、html页面跳转到另一个html页面传参数
angularjs规定页面跳转到页面并且传参数语法为: 页面#?参数名=参数值
二、html页面中接收另一个页面传过来的参数:
angularjs规定语法: $location.search()['参数名'];
$location是angularjs的内置对象, 里面有search方法, 用来搜索其他页面传入这里的参数

redis分布式缓存

redis底层使用C语言编写, 存储数据是放在内存中, 速度非常快.
作用:
redis在互联网项目中一般充当分布式缓存使用

业务流程:
获取数据的时候先从redis中获取, 如果获取到数据则直接返回, 就不用访问数据库了
如果获取不到数据, 可以从数据库中查询, 查询到后放入redis中一份, 下回就可以直接从redis中查询到,这样大大降低了数据库的高并发访问压力.

持久化方案:

  1. rdb(默认) 分时持久化
    可以在配置文件中设定, 多长时间持久化一次, 持久化次数少也就是操作硬盘的次数少,速度快. 但是如果在没有完成持久化前, 如果服务器断电, 则内存中没有持久化的数据会丢失.

  2. aof 实时持久化
    每次向redis中做增删改操作, 都会将数据持久化到硬盘上, 数据可靠性高, 不会丢失,
    但是速度慢

redis中数据类型: string, set, zset, list, hash

redis同类型技术:
memcache是redis的同类型技术, 底层也是使用c语言编写, 现在memcache已经被redis替代了.memcache的速度和redis相当. 但是memcache没有持久化方式.

mongodb和redis区别:
mongodb也是一个nosql数据库, 存储的数据是非结构化的, 但是mongdb和redis不能放在一起对比,因为完全没有可比性, 使用场景完全不同

  • redis: 主要使用内存, 有两种持久化方案, 速度非常快, 一般做分布式缓存使用
  • mongodb: 主要使用硬盘存储, 所以不会担心数据丢失, 速度介于redis和传统数据库之间.但是mongodb更擅长存储大文本数据, 以及一些非结构化数据, mongodb比redis的数据类型更加丰富.
    例如: 存储小说网站的小说, 存储电商网站的评论等这些数据

SpringDataRedis

Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:

  1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类
  2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
    ValueOperations:简单K-V操作
    SetOperations:set类型数据操作
    ZSetOperations:zset类型数据操作
    HashOperations:针对map类型的数据操作
    ListOperations:针对list类型的数据操作

操作string类型的数据

一、引入依赖

<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>

二、引入属性文件

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

三、引入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>

四、代码实现

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class TestString {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSet(){
        redisTemplate.boundValueOps("testKey").set("xxxxx");

    }

    @Test
    public void testGet(){
        String testKey = (String) redisTemplate.boundValueOps("testKey").get();
        System.out.println(testKey);
    }

    @Test
    public void testDelete(){
        redisTemplate.delete("testKey");
    }

}

操作hash类型的数据

redis就相当于一个大的hashmap,他的值是hash,那么value也相当于一个hashmap
相当于HashMap<String,HashMap<String,value>

Hash类型是无序的

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class TestHash {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testPut(){
        redisTemplate.boundHashOps("testHash").put("001","青龙");
        redisTemplate.boundHashOps("testHash").put("002","白虎");
        redisTemplate.boundHashOps("testHash").put("003","朱雀");
        redisTemplate.boundHashOps("testHash").put("004","玄武");
    }

    @Test
    public void testGetOne(){
        String value = (String) redisTemplate.boundHashOps("testHash").get("003");
        System.out.println(value);
    }

    @Test
    public void testGetAll(){
        Map<String,String> map = (Map<String,String>) redisTemplate.boundHashOps("testHash").entries();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"+"+value);
        }
    }

    @Test
    public void testDeleteOne(){
        redisTemplate.boundHashOps("testHash").delete("003");
    }

    @Test
    public void testDeleteAll(){
        redisTemplate.delete("testHash");
    }

}

操作list类型的数据

list有序,里面存入的数据可以重复

添加数据方式:左插入或右插入
左插入:相当于栈
右插入:相当于队列

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class TestList {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testLpush(){
        redisTemplate.boundListOps("testList").leftPush("1");
        redisTemplate.boundListOps("testList").leftPush("2");
        redisTemplate.boundListOps("testList").leftPush("3");
        redisTemplate.boundListOps("testList").leftPush("4");
    }

    @Test
    public void testRpush(){
        redisTemplate.boundListOps("testList").rightPush("1");
        redisTemplate.boundListOps("testList").rightPush("2");
        redisTemplate.boundListOps("testList").rightPush("3");
        redisTemplate.boundListOps("testList").rightPush("4");
    }

    @Test
    public void testRange(){
        List<String> testList = redisTemplate.boundListOps("testList").range(0, 10);
        for (String s : testList) {
            System.out.println(s);
        }
    }

    @Test
    public void testDelete(){
        redisTemplate.delete("testList");
    }

}

猜你喜欢

转载自blog.csdn.net/Sakuraaaaaaa/article/details/106615499