【Redis 2】入门

一、百度百科 

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。 

Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

二、Redis下载

【Redis 2】入门_第1张图片

三、Linux中安装Redis

1、上传、解压

Redis一般安装在Linux环境下,开启虚拟机,通过xftp将redis压缩包上传到Linux服务器,并进行解压。

【Redis 2】入门_第2张图片

2、修改redis.conf配置文件,使其在后台启动

【Redis 2】入门_第3张图片

四、Java调用redis

1、导入pom


    redis.clients
    jedis
    3.2.0

 2、编写Java主方法

调用Redis中的ping方法,惊现异常:

【Redis 2】入门_第4张图片

(1)开始的时候以为是防火墙的问题,后来通过查看redis状态发现IP地址不对,不应该是127.0.0.1

【Redis 2】入门_第5张图片

(2)修改redis.conf

【Redis 2】入门_第6张图片

注意:需要注意的是在修改redis.conf时,①注掉bind 127.0.0.1;②需要将本机访问保护模式设置为no

3、再次执行主方法,执行成功!

【Redis 2】入门_第7张图片

五、五大数据类型代码实例

package com.guor.redis;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class JedisTest01 {
    public static void main(String[] args) {
        test05();
    }

    private static void test01(){
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        String value = jedis.ping();
        System.out.println(value);
        //添加
        jedis.set("name","GooReey");
        //获取
        String name = jedis.get("name");
        System.out.println(name);

        jedis.set("age","30");
        jedis.set("city","dalian");
        //获取全部的key
        Set keys = jedis.keys("*");
        for(String key : keys){
            System.out.println(key+" --> "+jedis.get(key));
        }

        //加入多个key和value
        jedis.mset("name1","zs","name2","ls","name3","ww");
        List mget = jedis.mget("name1", "name2");
        System.out.println(mget);//[zs, ls]
    }

    //list
    private static void test02(){
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        jedis.lpush("key1","01","02","03");
        List values = jedis.lrange("key1",0,-1);
        System.out.println(values);//[03, 02, 01]
    }

    //set
    private static void test03(){
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        jedis.sadd("username","zs","ls","ww");
        Set names = jedis.smembers("username");
        System.out.println(names);//[ww, zs, ls]
    }

    //hash
    private static void test04(){
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        jedis.hset("users","age", "20");
        String hget = jedis.hget("users","age");
        System.out.println(hget);
    }

    //zset
    private static void test05(){
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        jedis.zadd("china",100d,"shanghai");
        Set names = jedis.zrange("china",0,-1);
        System.out.println(names);//[shanghai]
    }
}

六、手机验证码功能代码实例

package com.guor.redis;

import redis.clients.jedis.Jedis;

import java.util.Random;

public class PhoneCode {
    public static void main(String[] args) {
        verifyCode("10086");//795258
        getRedisCode("10086","795258");//success.
    }

    //1、生成6位数字验证码
    public static String getCode(){
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            int rand = random.nextInt(10);
            code += rand;
        }
        return code;//849130
    }

    //2、每个手机每天只能发送三次,验证码放到redis中,设置过期时间
    public static void verifyCode(String phone){
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        //拼接key
        //手机发送次数key
        String countKey = "VerifyCode" + phone + ":count";
        //验证码key
        String codeKey = "VerifyCode" + phone + ":code";
        //每个手机每天只能发送三次
        String count = jedis.get(countKey);
        if(count == null){
            //设置过期时间
            jedis.setex(countKey,24*60*60,"1");
        }else if(Integer.parseInt(count)<=2){
            //发送次数+1
            jedis.incr(countKey);
        }else if(Integer.parseInt(count)>2){
            System.out.println("今天的发送次数已经超过三次");
            jedis.close();
        }

        String vCode = getCode();
        jedis.setex(codeKey,120,vCode);
        jedis.close();
    }

    //3、验证码校验
    public static void getRedisCode(String phone, String code){
        //从redis中获取验证码
        Jedis jedis = new Jedis("192.168.194.131", 6379);
        //验证码key
        String codeKey = "VerifyCode" + phone + ":code";
        String redisCode = jedis.get(codeKey);
        if(redisCode.equals(code)){
            System.out.println("success.");
        }else{
            System.out.println("error");
        }
        jedis.close();
    }
}

【Redis 2】入门_第8张图片

当超过三次时: 

【Redis 2】入门_第9张图片

七、SpringBoot整合Redis

1、建工程,引入pom

【Redis 2】入门_第10张图片



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    com.guor
    redisspringboot
    0.0.1-SNAPSHOT
    redisspringboot
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
            2.4.5
        

        
        
            org.apache.commons
            commons-pool2
            2.9.0
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、配置类

(1)application.properties

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.194.131
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000

(2)RedisConfig

package com.guor.redisspringboot.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import java.time.Duration;

@EnableCaching
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 基于SpringBoot2 对 RedisCacheManager 的自定义配置
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //设置CacheManager的值序列化方式为json序列化
        RedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
        RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);

        //设置默认超过时期是1天
        defaultCacheConfig.entryTtl(Duration.ofDays(1));
        //初始化RedisCacheManager
        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
    }
}

3、控制类测试

package com.guor.redisspringboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redisTest")
public class RedisTestController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public String getRedis(){
        redisTemplate.opsForValue().set("name","zs");
        String name = (String) redisTemplate.opsForValue().get("name");
        return name;
    }
}

4、测试

【Redis 2】入门_第11张图片

 

往期精彩内容:

Java知识体系总结(2021版)

Java多线程基础知识总结

【全栈最全Java框架总结】SSH、SSM、Springboot

超详细的springBoot学习笔记

常见数据结构与算法整理总结

Java设计模式:23种设计模式全面解析

Java面试题总结(附答案)

你可能感兴趣的:(Redis)