Redis学习

Redis

一、简介

1.1 NoSQL

泛指非关系型的数据库,有四大类:

  • 键值(Key-Value)存储数据库:Redis,memcache
  • 列存储数据库:HBase
  • 文档型数据库:MongoDB
  • 图形(Graph)数据库:InfoGrid

特点:

  • 数据模型比较简单
  • 性能较好
  • 不需要高度的数据一致性 ACID

1.2 Redis

Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库。

优点:

  • 高并发读写
  • 对海量数据存储和访问效率高
  • 可扩展性好
  • 高可用

缺点:

  • ACID处理非常简单
  • 无法做到太复杂的关系数据模型

二、Redis基础

1. 下载与安装

下载地址:https://redis.io/

window版本:https://github.com/microsoftarchive/redis/releases

客户端启动指定IP和端口

redis-cli -h 127.0.0.1 -p 6380

安装成服务的命令

redis-server --service-install redis.windows.conf

2. 数据类型

  • String
  • List
  • Set
  • SortedSet
  • Hash

3. 常用命令

命令参考 http://doc.redisfans.com/

3.1 数据库命令

  • ping 测试与服务器的连接是否仍然生效,正常会返回一个 PONG
  • select 切换到指定的数据库
  • flushdb 清空当前数据库中的所有 key
  • dbsize 返回当前数据库的 key 的数量

3.2 Key

  • keys * 列出所有的键
  • exists 检查给定 key 是否存在
  • move
  • expire
  • ttl
  • del
  • type

3.3 String

  • set/get
  • incr/decr
  • setex
  • setnx
  • mget/mset

3.4 Hash

  • hset/hget
  • hmset/hmget
  • hgetall
  • hexists
  • hlen
  • hkeys/hvals

3.5 List

  • lpush/rpush/lrange
  • lpop/rpop
  • lindex
  • llen

3.6 Set

  • sadd
  • smembers
  • scard
  • spop

3.7 SortedSet

  • zadd
  • zrange
  • zrangebyscore
  • zcard
  • zrem

4. Java操作Redis

使用Jedis驱动操作

步骤:

  1. pom.xml

    <dependency>
    	    <groupId>redis.clients</groupId>
    	    <artifactId>jedis</artifactId>
    	    <version>2.10.2</version>
    	</dependency>
    	
    	<dependency>
    		<groupId>junit</groupId>
    		<artifactId>junit</artifactId>
    		<version>4.12</version>
    		<scope>test</scope>
    	</dependency>
    
  2. 测试类

    @Test
    public void testJedis() {
        Jedis client = new Jedis("127.0.0.1", 6380);
    
        System.out.println(client.ping());
    
        //		client.set("company", "zhongruan");
    
        System.out.println(client.get("addr"));
    
    }
    

5. 持久化

  • RDB(Redis DataBase)

    • 按一定间隔将内存中的数据写入磁盘,

    • 性能好

    • 数据可能丢失

    • 默认开启

      save 900 1
      save 300 10
      save 60 10000
      
  • AOF(Append Only File)

    • 以日志的形式按顺序记录写操作

    • 类似Oracle的redo日志

    • 数据不会丢失

    • 性能有损耗

    • 日志量大, 需要定期压缩维护

    • 默认不开启

      appendonly no
      

三、Redis高级

1. 主从同步

读写分离

在从机用命令配置为从库 slaveof host port

查看 info replication

恢复成master slaveof no one

哨兵模式

2. 高可用集群

四、应用实例

1. Mybatis二级缓存

pom.xml

<!-- redis -->
<dependency>
    <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.6.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.0.0</version>
</dependency>

RedisCache.java

RedisCacheTransfer.java

spring-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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" 
    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/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

     <!-- redis连接池配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="1024"/>
        <property name="maxIdle" value="200" />
        <property name="testOnBorrow" value="true"/>
    </bean>
    
    <!-- redis连接工厂 -->
    <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="poolConfig"  ref="poolConfig"></property> 
    </bean>
    
    <!-- redis操作模板,使用该对象可以操作redis  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <!--开启事务  -->
        <property name="enableTransactionSupport" value="true"/>
    </bean>

    <!-- 使用中间类解决RedisCache.RedisTemplate的静态注入,从而使MyBatis实现第三方缓存 -->
    <bean id="redisCacheTransfer" class="com.veryoo.utils.RedisCacheTransfer">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
</beans>

web.xml 或 applicationContext.xml

UserDao.xml 或UserDao.java

<cache type="com.veryoo.utils.RedisCache"></cache>
@CacheNamespace(implementation = RedisCache.class)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值