Home >Java >javaTutorial >What is the method for integrating Redis in java SpringBoot project?

What is the method for integrating Redis in java SpringBoot project?

王林
王林forward
2023-05-12 13:52:131536browse

Redis is completely open source, complies with the BSD protocol, and is a high-performance key-value database.

Redis has the following three characteristics with other key-value cache products:

  • Redis supports data persistence. It can save the data in the memory to the disk and load it again for use when restarting.

  • Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.

  • Redis supports data backup, that is, data backup in master-slave mode.

Advantages of Redis

  • Extremely high performance– Redis can read at a speed of 110,000 times/s and write at a speed of 81,000 times/s s

  • Rich data types - Redis supports String, List, Hash, Set and zset data type operations in binary cases.

  • Atomic – All operations of Redis are atomic, which means that they are either executed successfully or not executed at all. Individual operations are atomic. Multiple operations also support transactions, that is, atomicity, wrapped by the MULTI and EXEC instructions.

  • Rich features– Redis also supports publish/subscribe, notification, key expiration and other features

  • Redis is single-threaded, 6.0 This version supports enabling multi-threading.

Redis installation

java SpringBoot项目整合Redis的方法是什么

Decompress the downloaded compressed file. The decompressed file list is as follows:

java SpringBoot项目整合Redis的方法是什么

Use cmd window to open Redis

redis-server.exe   redis.windows.conf   #加载配置文件启动

java SpringBoot项目整合Redis的方法是什么

Note: After starting, do not close the window, the service will stop when you close the window!

Install the Redis database client

java SpringBoot项目整合Redis的方法是什么

##Library related instructions:

flushdb  清空当前库

flushall  清空所有库

select   1    切换库

Key related instructions

InstructionsFunctionSyntaxDelete one or more keysdel keyname Determine whether one or more keys exist. If one of multiple keys exists, 1 will be returned.exists keynameSet the key survival time unit: secondsexpire keyname secondsQuery all keys matching the pattern? Match one character * Match 0-n characters [] Meet one of the key * key h?lloMove the key to the specified librarymove keyname db##pexpirettlrandomkeyrenametype

Redis data type

1.String (string)

  • string is the most basic type of redis, you can understand it as exactly the same type as Memcached, a key corresponds to a value.

  • The string type is binary safe. This means that the string of redis can contain any data. For example, jpg images or serialized objects.

  • The string type is the most basic data type of Redis. The string type value can store up to 512MB.

Operation instructions:

del
exists
expire
keys
move
Set the key's survival time Unit: millisecond setting returns 1 if successful, otherwise returns 0 pexpire keyname milliseconds
Return key in seconds The remaining survival time, returns -1 for permanent storage, -2 for key does not exist ttl keyname
Return a random key from the current database randomkey
Rename the key and return ok successfully. Otherwise, an error message is returned. rename key newkey
Return the type of value stored in key type keyname
Add the key The stored value is added to the given floating point increment value (increment) Decrease the numeric value stored in key one. The stored value minus the given decrementIf the key already exists and is a string, the APPEND command will append the specified value to the end of the original value of the key
Command Description
SET Set the value of the specified key
GET Get Specify the value of key.
GETRANGE Returns the subcharacters of the string value in key
GETSET Set the value of the given key to value and return the old value of the key.
SETEX Associate value value to key and set key's expiration time to seconds (in seconds).
SETNX Set the value of key only when the key does not exist
STRLEN Returns the length of the string value stored in key.
MSET Set one or more key-value pairs at the same time.
MSETNX Set one or more key-value pairs simultaneously, if and only if all given keys do not exist
INCR Increase the numerical value stored in the key by one
INCRBY Add the given increment value (increment) to the value stored in the key
##INCRBYFLOAT
DECR
DECRBY
APPEND
2.Hash (Hash)

  • Redis hash is a collection of key-value (key=>value) pairs.

  • Redis hash is a mapping table of string type fields and values. Hash is particularly suitable for storing objects.

Operation instructions:

CommandDescriptionhsetSet a key/value pairhgetGet the value corresponding to the keyhgetallGet all key/value pairshdelDelete a key/value pairhexistsDetermine whether a key existshkeysGet all keyshvalsGet all valueshmsetSet multiple key/valuehmgetGet the value of multiple keyshsetnxSet the value of a non-existent keyhincrbyPerform addition operation for value valuehincrbyfloatPerform addition operation for floating point type value for value value

3.List (List)

  • Redis list is a simple list of strings, sorted in insertion order. You can add an element to the head (left) or tail (right) of the list.

Operation instructions

Get the elements in the list by index lindex lists 0Insert elements before or after the elements in the listGet the length of the listMove out and get the first element of the listInsert one or more values ​​into the head of the listInsert a value into the Get the elements within the specified range of the list (0 -1)Remove duplicate elements from the listSet the value of the list element through the index, but the index must exist. The essence is to modify the value based on the index Trim a list, that is, let the list only retain elements within the specified range, and elements that are not within the specified range will be deletedRemove the last element of the list, the return value is the removed elementRemove the last element of the list and add that element to another list and return Add in list One or more valuesAdd a value to an existing list
Command Description
##LINDEX
LINSERT key BEFORE|AFTER
LLEN
LPOP
LPUSH
LPUSHX existing list header
LRANGE
LREM
LSET
LTRIM
RPOP
RPOPLPUSH
RPUSH
RPUSHX
4.Set (set)

  • #Redis’ Set is an unordered collection of string type.

  • Collections are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).

Operation instructions:

CommandDescriptionsaddAdd elements to the collectionsmembersDisplay all elements in the collection (unordered)scardReturn the number of elements in the collectionspopReturn an element randomly and delete this element smoveTransfer elements from a set to a setsremFrom a set Delete an element from sismemberDetermine whether the set contains this elementsrandmemberRandomly Return an elementsinterFind the intersectionsunionFind the sum set
5.ZSet (sorted set: ordered set)

  • Redis ZSet and Set It is also a collection of String type elements, and duplicate members are not allowed.

  • The difference is that each element is associated with a

    double type of score. Redis It is through scores that the members in the set are sorted from small to large. The members of

  • ZSet are unique, but the scores can be repeated.

Operation instructions:

CommandDescriptionzaddAdd an ordered set elementzcardReturn the number of elements in the setzrange ascending order zrevrange descending order Return elements within a range zrangebyscoreFind elements within a range by score zrankReturn to rankingzrevrankFlashback rankingzscoreDisplay the score of an elementzremRemove an elementzincrbyAdd points to a specific element

SpringBoot 操作 Redis

  spring boot data redis中提供了RedisTemplate和StringRedisTemplate,其中StringRedisTemplate是Redistemplate的子类,两个方法基本一致,不同之处主要体现在操作的数据类型不同,RedisTemplate中的两个泛型都是Object,意味着存储的key和value都可以是一个对象,而StringRedisTemplate的两个泛型都是String,意味着StringRedisTemplate的key和value都只能是字符串。

引入依赖

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

SpringBoot 配置 Redis

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

RedisTemplate 及其相关方法

1.RedisTemplate 介绍

  Spring封装了RedisTemplate对象来进行对Redis的各种操作,它支持所有的Redis原生的api。RedisTemplate位于spring-data-redis包下。RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅。

2.Redis 5种数据结构操作

  • redisTemplate.opsForValue(); //操作字符串

  • redisTemplate.opsForHash(); //操作hash

  • redisTemplate.opsForList(); //操作list

  • redisTemplate.opsForSet(); //操作set

  • redisTemplate.opsForZSet(); //操作有序set

或者:

  • redistempalate.boundValueOps

  • redistempalate.boundSetOps

  • redistempalate.boundListOps

  • redistempalate.boundHashOps

  • redistempalate.boundZSetOps

 opsForXXX和boundXXXOps的区别:XXX为value的类型,前者获取一个operator,但是没有指定操作的对象(key),可以在一个连接(事务)内操作多个key以及对应的value;后者获取了一个指定操作对象(key)的operator,在一个连接(事务)内只能操作这个key对应的value。

 SpringBootTest 实现Redis数据库增删改查

/**
 * 使用RedisTemplate 操作Redis数据的不同数据类型
 */
@SpringBootTest
public class Springbootday03ApplicationTests {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * String 类型数据操作
     */
    @Test
    public void operateString() {

        //添加值
        redisTemplate.opsForValue().set("str", "strValue1");

        //添加值  判定是否存在 存在则不添加
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("str", "strAbsent");
        System.out.println("str设置成功:" + aBoolean);

        //获取值
        String str = redisTemplate.opsForValue().get("str");
        System.out.println("str = " + str);

        //更新值
        redisTemplate.opsForValue().set("str", "strValue2");
        str = redisTemplate.opsForValue().get("str");
        System.out.println("newStr = " + str);

        //删除值
        Boolean b = redisTemplate.delete("str");
        System.out.println("str删除成功:" + b);

    }

    /**
     * 操作string类型数据  设置过期时间
     */
    @Test
    public void operateString2() {
        redisTemplate.opsForValue().set("str", "strTimeout", 10, TimeUnit.SECONDS);
        //判定值是否存在 不存在则设置值 同时设置过期时间
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("str2", "strTimeoutAbsent", 20, TimeUnit.SECONDS);
        System.out.println("setIfAbsent:" + aBoolean);
    }

    /**
     * 操作hash类型数据
     */
    @Test
    public void operateHash() {
        //添加hash类型数据  key - value
        redisTemplate.opsForHash().put("hash", "username", "admin");
        //修改hash类型数据
        redisTemplate.opsForHash().put("hash", "username", "tom");
        redisTemplate.opsForHash().put("hash", "password", "123456");

        //添加hash类型数据  key - map
        HashMap<String, String> map = new HashMap<>();
        map.put("driverName", "com.mysql.jdbc.Driver");
        map.put("url", "jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC");
        redisTemplate.opsForHash().putAll("hash", map);

        //获取hash类型数据  entries
        Map<Object, Object> hash = redisTemplate.opsForHash().entries("hash");
        hash.forEach((key, value) -> {
            System.out.println(key + "::" + value);
        });

        //获取所有的key
        Set<Object> keys = redisTemplate.opsForHash().keys("hash");
        for (Object key : keys) {
            System.out.println("key:" + key);
        }
        //获取所有value
        List<Object> values = redisTemplate.opsForHash().values("hash");
        values.forEach(value -> System.out.println("value:" + value));

        //删除hash类型数据  删除一个  返回删除的个数
        Long delete = redisTemplate.opsForHash().delete("hash", "username");
        System.out.println("delete = " + delete);

        //删除hash类型数据  删除多个  返回删除的个数
        delete = redisTemplate.opsForHash().delete("hash", "username", "password", "driverName");
        System.out.println("delete = " + delete);

        //删除hash类型数据  删除所有
        Boolean delHash = redisTemplate.delete("hash");
        System.out.println("delHah:" + delHash);

    }

    /**
     * 操作List类型  有序 可重复
     */
    @Test
    public void operateList() {

        //左压栈
        // redisTemplate.opsForList().leftPush("list", "listValue1");
        // redisTemplate.opsForList().leftPush("list", "listValue1");
        // redisTemplate.opsForList().leftPush("list", "listValue2");
        // redisTemplate.opsForList().leftPush("list", "listValue3");

        //右压栈
        redisTemplate.opsForList().rightPush("list", "listValue0");
        redisTemplate.opsForList().rightPush("list", "listValue2");
        redisTemplate.opsForList().rightPush("list", "listValue0");

        //左出栈
        String list1 = redisTemplate.opsForList().leftPop("list");
        System.out.println("leftPop list1 = " + list1);
        //右出栈
        String list2 = redisTemplate.opsForList().rightPop("list");
        System.out.println("rightPop list2 = " + list2);

        //获取所有数据
        List<String> lists = redisTemplate.opsForList().range("list", 0, 		        redisTemplate.opsForList().size("list") - 1);
        lists.forEach(list -> System.out.println(list));


        //设置指定位置的数据
        redisTemplate.opsForList().set("list", 0, "listValue0");
        /**
         * 从存储在键中的列表中删除等于值的元素的第一个计数事件。
         * count> 0:删除等于从左到右移动的值的第一个元素;
         * count< 0:删除等于从右到左移动的值的第一个元素;
         * count = 0:删除等于value的所有元素。
         */
        Long remove = redisTemplate.opsForList().remove("list", -1, "listValue0");
        System.out.println("remove:" + remove);

        //删除指定key的list数据
        Boolean list = redisTemplate.delete("list");
        System.out.println("list集合删除成功:" + list);
    }

    /**
     * 操作Set类型  无序 不可重复
     */
    @Test
    public void operateSet() {

        //设置set值
        redisTemplate.opsForSet().add("set", "setValue0");
        redisTemplate.opsForSet().add("set", "setValue0");
        redisTemplate.opsForSet().add("set", "setValue1");

        //判定是否包含
        Boolean member = redisTemplate.opsForSet().isMember("set", "setValue0");
        System.out.println("isMember:" + member);

        //删除set中的值
        Long remove = redisTemplate.opsForSet().remove("set", "setValue0");
        System.out.println("remove = " + remove);

        //获取set类型值
        Set<String> set = redisTemplate.opsForSet().members("set");
        set.forEach(str -> {
            System.out.println("str = " + str);
        });
    }

    /**
     * 操作 ZSet  有序 不可重复
     */
    @Test
    public void operateZSet() {
        //存储值
        Boolean add = redisTemplate.opsForZSet().add("zset", "zsetValue0", 10);
        System.out.println("add = " + add);
        System.out.println("add = " + add);
        add = redisTemplate.opsForZSet().add("zset", "zsetValue2", 2);
        System.out.println("add = " + add);
        //获取值
        // Boolean zset = redisTemplate.delete("zset");
        // System.out.println("delete zset = " + zset);
    }
}

Redis工具类的封装

/**
 * Redis 工具类
 * @author mosin
 * date 2021/11/30
 * @version 1.0
 */
@Component
public final class RedisUtil {

    private RedisUtil(){};
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //设置值
    public void  setValue(String key,String value){
        redisTemplate.opsForValue().set(key, value);
    }
    // 设置值 同时设置有效时间
    public void setValue(String key, String value, Long timeOut, TimeUnit timeUnit){
        redisTemplate.opsForValue().setIfAbsent(key, value, timeOut, timeUnit);
    }

    //设置值 没有则设置 有则不设置
    public void  setNx(String key,String value){
        redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    //设置值 没有则设置 同时设置有效时间 有则不设置
    public void  setNx(String key,String value,long timeOut,TimeUnit timeUnit){
        redisTemplate.opsForValue().setIfAbsent(key, value,timeOut,timeUnit);
    }

    //删除值
    public boolean del(String key){
        return redisTemplate.delete(key);
    }
    
     //获取值
    public String getValue(String key){
        return  redisTemplate.opsForValue().get(key);
    }
}

Redis 业务实践

redis 存储 token,实现非法请求拦截

1.编写拦截器

@Component
public class AdminInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器以拦截请求");
        //从请求头中获取token  验证用户是否登录
        String token = request.getHeader("token");
        System.out.println(token);
        String tokenValue = redisUtil.getValue(token);
        System.out.println("tokenValue = " + tokenValue);
        if(tokenValue!=null){ //用户已登录 放行请求
            return  true;
        }else{//重定向到登录页面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;
        }
    }
}

2.配置拦截器

@Configuration
public class LoginConfig implements WebMvcConfigurer {
    @Autowired
    private AdminInterceptor adminInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(adminInterceptor);
        registration.addPathPatterns("/**");
        registration.excludePathPatterns("/user/login","/user/register","/login.jsp");
    }
}

3.编写统一返回数据格式类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class JsonResult<T> {
    private Integer code;
    private String msg;
    private Long count;
    private T data;
}

4.编写控制器

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @ResponseBody
    @RequestMapping("/login")
    public Object login(User user) throws JsonProcessingException {
        User usr = User.builder().id(1).name("admin").password("123456").build();
        //获取token  放入redis
        String token = UUID.randomUUID().toString().replace("-", "");
        //将user 转为json格式放入 redis
        ObjectMapper objectMapper = new ObjectMapper();
        String s1 = objectMapper.writeValueAsString(usr);
        //将 token 和用户信息存入 redis
        redisUtil.setValue(token, s1, 2L, TimeUnit.MINUTES);
        //将token 存入map集合返回
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        return map;
    }

    @ResponseBody
    @RequestMapping("/register")
    public Object register(User user){
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "ok");
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Object add(User user){
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "ok");
        return map;
    }
}

5.编写业务类和Mapper接口

6.使用postman接口测试工具测试接口

The above is the detailed content of What is the method for integrating Redis in java SpringBoot project?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete