Redis的使用--Java连接redis、springboot整合redis、缓存 分布锁 、redssion解决分布锁的bug(超时问题) 以及redis常见面试题

1.redis支持哪些语言可以操作

2.Java连接redis

2.1使用jedis

(1)添加jedis依赖

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.8.0</version>
        </dependency>

(2)测试

连接redis---->必须保证你的redis服务允许远程连接

bind 0.0.0.0 87行

@Test
    public void test01(){
        //连接redis--->必须保证你的redis服务运行远程连接。
        //该对象中把每个redis命令封装成对应的方法了。
        Jedis jedis=new Jedis("192.168.223.155",6380);
        //对于字符串操作的命令
        String s = jedis.set("k1", "v1");
        System.out.println(s);
        String setex = jedis.setex("k2", 30l, "v2");
        System.out.println(setex);
        Long aLong = jedis.setnx("k3", "v11");
        System.out.println(aLong);

        //对于hash操作
        jedis.hset("k4","name","刘德华");
        jedis.hset("k4","age","15");

        Map<String,String> map=new HashMap();
        map.put("name","张需要");
        map.put("age","28");
        jedis.hset("k5",map);


        jedis.close();

    }

 2.2使用连接池连接redis

public void test02(){
        //创建连接池的配置类
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(20);
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxWait(Duration.ofMillis(3000));
        JedisPool jedisPool=new JedisPool(jedisPoolConfig,"192.168.223.155",6380);

        long start=System.currentTimeMillis();

        for (int i = 0; i <1000 ; i++) {
            //从jedis连接池获取资源
            Jedis jedis = jedisPool.getResource();
            String ping = jedis.ping();
            jedis.close();//是否资源到池子
        }

        long end=System.currentTimeMillis();

        System.out.println("总耗时:"+(end-start));

    }

 注意:如果是ssm框架

<bean id="" class="JedisPoolConfig">

​ <property name="maxIdle" value="20">

</bean>    然后用autowired创建对象

2.3 java连接redis集群模式

@Test
    public void test03(){
        Set<HostAndPort> nodes=new HashSet<>();
        nodes.add(new HostAndPort("192.168.223.155",6001));
        nodes.add(new HostAndPort("192.168.223.155",6002));
        nodes.add(new HostAndPort("192.168.223.155",6003));
        nodes.add(new HostAndPort("192.168.223.155",6004));
        nodes.add(new HostAndPort("192.168.223.155",6005));
        nodes.add(new HostAndPort("192.168.223.155",6006));
        JedisCluster jedisCluster=new JedisCluster(nodes);

        jedisCluster.set("k6","刘德华和闫克起");

        jedisCluster.close();

    }

 3.springboot整合redis

springboot对redis的操作封装了两个StringRedisTemplate和RedisTemplate类,StringRedisTemplate是RedisTemplate的子类,StringRedisTemplate它只能存储字符串类型,无法存储对象类型。要想用StringRedisTemplate存储对象必须把对象转为json字符串。

StringRedisTemplate

(1) 引入相关的依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

(2)注入StringRedisTemplate该类对象

@Autowired
 private StringRedisTemplate redisTemplate;

(3)使用StringRedisTemplate

该类把对每种数据类型的操作,单独封了相应的内部类。  

package com.qy151.qy151redisspringboot;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class Qy151RedisSpringbootApplicationTests {

    //里面所有的key还是value field它的类型必须都是String类型。
    //因为key和value获取field他们使用的都是String的序列化方式
    @Autowired
    private StringRedisTemplate redisTemplate;


    @Test
    public void test02(){
        //对hash类型的操作。
        HashOperations<String, Object, Object> forHash = redisTemplate.opsForHash();
        forHash.put("k1","name","张三");
        forHash.put("k1","age","15");
        Map<String,String> map=new HashMap<>();
        map.put("name","李四");
        map.put("age","25");
        forHash.putAll("k2",map);

        Object o = forHash.get("k1", "name");
        System.out.println(o);

        Set<Object> k1 = forHash.keys("k1");
        System.out.println(k1);
        List<Object> k11 = forHash.values("k1");
        System.out.println(k11);


        //获取k1对于的所有的field和value
        Map<Object, Object> k12 = forHash.entries("k1");
        System.out.println(k12);

    }

    @Test
    void contextLoads() {
        //删除指定的key
//        redisTemplate.delete();
        //查看所有的key
//        redisTemplate.keys()
        //是否存在指定的key
//         redisTemplate.hasKey()

        //对字符串数据类型的操作ValueOpe
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值