1.Redis初步了解

1.1简介

数据库存储的弊端:

  1. 数据库存储是面向磁盘的,磁盘的读写比较慢.(但是它廉价啊,一般都是TG级别)
  2. 当高并发(秒杀)时候,一瞬间成千上万的请求过来(连接不能及时释放),及其容易造成数据库系统瘫痪.服务器宕机

 

NoSql

Nosql工具也是简易的数据库,是基于内存的数据库,有一定的持久化(但是内存贵啊,几百个G都是大的了),主要有Redis和MongoDB

 

Redis优点:

Redis的性能可以达到每秒十几万次的读/写操作.

Redis是基于ANSIC语言,接近汇编语言的机器语言,运行迅速.

基于内存的读写,速度比数据库快.

只有6中数据结构,规则简单,数据结构简单.

 

 

什么时候使用Redis:

  1. 常用业务数据
  2. 业务数据的读操作比写操作多
  3. 业务数据的大小

 

Redis逻辑

 

图像 小部件

Redis在高速读写场合运用

 

  1. 当一个请求到达服务器的时候,只是把业务数据在redis中进行读写,没有任何对数据库的操作.
  2. 当请求完成读写后,会判断高速业务是否完成(比如秒杀商品的个数为0,抢红包金额为0).
  3. 如果高速业务完成,会将缓存中的数据批量写入到数据库

 

Redis和数据库的异同

  1. Redis存储在内存(部分可以持久化)数据库存储在磁盘.所以Redis不安去,一旦停电或者服务器故障,很容易丢失数据
  2. Redis的数据结构比较简单,不如数据库sql语句强大,计算能力强

 

1.2 Redis的使用

1.2.1 Linux安装Redies

  1. sudo apt-get update
  2. sudo apt-get install redis-server
  3. redis-server

 

1.2.2 java中使用Redis

 

Java提供了redis.clients.jedis.JedisPool来创建redis连接池对象

使用JedisPool需要使用 redis.clients.jedis.JedisPoolConfig来对连接池进行配置.


 

        JedisPoolConfig config = new JedisPoolConfig();

        /*配置连接池最大空闲数*/

        config.setMaxIdle(50);



        /*配置连接池最大连接数*/

        config.setMaxTotal(100);



        /*最大等待毫秒数*/

        config.setMaxWaitMillis(20000);



        /*使用配置创建连接池*/

        JedisPool jedisPool = new JedisPool(config, "localhost");



        /*从连接池中获取单个链接*/

        Jedis jedis = jedisPool.getResource();

1.2.3 spring中使用redis

 

1.2.3.1 配置连接工厂

使用RedisTemplate(管理链接-关闭)前,需要配置spring提供的连接工厂,在spring redis中提供了4中工厂模型(它们都是RedisConnectionFactory的实现类).常用的是JedisConnectionFactory

 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

        <property name="hostName" value="localhost"/>

        <property name="port" value="6379"/>

        <property name="poolConfig" ref="poolConfig"/>

</bean>

 

1.2.3.2  对象序列化

普通的连接没法把java对象直接存入到redies里.往往我们将对象序列化,进行redies转存.

Spring中提供了几个实现RedisSerializer接口的实现类

 

 

 

 

 

 

1.2.3.3  配置RedisTemplate

假设我们使用StringRedisSerializer进行key序列化,JdkRedisSerializer进行value序列化.

 <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>

    

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

        <property name="connectionFactory" ref="connectionFactory"/>

        <property name="keySerializer" ref="stringRedisSerializer"/>

        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>

</bean>

 

实例:

  1. 配置pom文件
   <groupId>redis.clients</groupId>

      <artifactId>jedis</artifactId>

      <version>2.9.0</version>

    </dependency>

    <dependency>

      <groupId>org.springframework.data</groupId>

      <artifactId>spring-data-redis</artifactId>

      <version>1.8.4.RELEASE</version>

    </dependency>

 

2.配置JedisPoolConfig和工厂连接


 

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">

        <property name="maxIdle" value="50"/>

        <property name="maxTotal" value="100"/>

        <property name="maxWaitMillis" value="20000"/>

</bean>

 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

        <property name="hostName" value="localhost"/>

        <property name="port" value="6379"/>

        <property name="poolConfig" ref="poolConfig"/>

    </bean>

  • 配置RediesTemplate
 <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>



    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>



    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

        <property name="connectionFactory" ref="connectionFactory"/>

        <property name="keySerializer" ref="stringRedisSerializer"/>

        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>

    </bean>

 

 

  1. 实现序列化的Role.java
public class Role implements Serializable {

    private int id;

    private String name;

private SexEnum sex;

****setter and getter******

}

运行方法

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring-mvc-redis.xml")

public class test1 {

    @Autowired

    private RedisTemplate redisTemplate;



    @Test

    public void test2() {

        Role role = new Role();

        role.setId(99);

        role.setName("dsafd");

        redisTemplate.opsForValue().set("role_1", role);

        Role role1 = (Role) redisTemplate.opsForValue().get("role_1");

        System.out.println(role1.toString());

        System.out.println(role.hashCode());

        System.out.println(role1.hashCode());

    }

}

结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值