在SpringBoot中使用SpringDataRedis

news/2024/7/6 2:59:23

SpringDataRedis应用:
说明:
关于Redis:一个基于键值对存储的NoSQL内存数据库,可存储复杂的数据结构,如List, Set, Hashes。
关于Spring Data Redis:简称SDR, 能让Spring应用更加方便配置和访问Redis。
1.在pom.xml加入以下依赖
<dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
2.配置RedisTemplateBean
主要生成两个bean,JedisConnectionFactory 和 RedisTemplate,RedisTemplate bean用于后续注入到service中操作Redis数据库。
@Bean(name = "redisTemplate")
  public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
    final RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.afterPropertiesSet();
    return template;
  }
3.使用RedisTemplate操作Redis
 /**
   * 将用户存储到redis中.
   * 
   * @param client
   */
  public static void setCurrentUser(TblUser user) {
    RedisTemplate<Object, Object> redisTemplate = SpringContextHolder.getBean("redisTemplate");
    redisTemplate.opsForValue().set(TBL_USER + user.getToken(), new Gson().toJson(user), 30, TimeUnit.DAYS);
  }


http://lihuaxi.xjx100.cn/news/244557.html

相关文章

理解RESTful架构

越来越多的人开始意识到&#xff0c;网站即软件&#xff0c;而且是一种新型的软件。 这种"互联网软件"采用客户端/服务器模式&#xff0c;建立在分布式体系上&#xff0c;通过互联网通信&#xff0c;具有高延时&#xff08;high latency&#xff09;、高并发等特点。…

JavaScript 复习之数据类型

一、简介 JavaScript 的数据类型有 7 种&#xff1a; 数值&#xff08;number&#xff09;字符串&#xff08;string&#xff09;布尔值&#xff08;boolean&#xff09;undefinednull对象&#xff08;object&#xff09;Symbol&#xff08;ES6 中新增的类型&#xff0c;表示独…

SpringRestTemplate用法详解

REST&#xff08;RepresentationalState Transfer&#xff09;是Roy Fielding 提出的一个描述互联系统架构风格的名词。REST定义了一组体系架构原则&#xff0c;您可以根据这些原则设计以系统资源为中心的Web 服务&#xff0c;包括使用不同语言编写的客户端如何通过 HTTP处理和…

PostgreSQL运维实战精讲之“postgresql源码安装”

一、下载地址wget https://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.gz二、安装&#xff1a;#安装依赖包yuminstall -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devellibxml2-devel libxslt-devel openldap-devel python-devel gcc-c openssl…

优秀Java程序员应该知道的20个实用开源库

一个优秀且经验丰富的Java开发人员的特点之一是对API的广泛了解&#xff0c;包括JDK和第三方库。我花了很多时间学习API&#xff0c;特别是在阅读Effective Java 3rd Edition之后&#xff0c;Joshua Bloch建议如何使用现有的API进行开发&#xff0c;而不是为常用的东西写新的代…

OpenLDAP自定义属性的启用

2019独角兽企业重金招聘Python工程师标准>>> # ucode# This multivalued field is used to record the values of the license or# registration plate associated with an individual.attributetype ( 2.16.840.1.113730.3.1.900 NAME ucode DESC user code …

在SpringBoot中使用Spring Session解决分布式会话共享问题

在SpringBoot中使用Spring Session解决分布式会话共享问题 问题描述&#xff1a; 每次当重启服务器时&#xff0c;都会导致会员平台中已登录的用户掉线。这是因为每个用户的会话信息及状态都是由session来保存的&#xff0c;而session对象是由服务器创建&#xff0c;并把sessio…

Linux堆内存管理深入分析(上)

Linux堆内存管理深入分析(上半部) 作者&#xff1a;走位阿里聚安全 0 前言 近年来&#xff0c;漏洞挖掘越来越火&#xff0c;各种漏洞挖掘、利用的分析文章层出不穷。从大方向来看&#xff0c;主要有基于栈溢出的漏洞利用和基于堆溢出的漏洞利用两种。国内关于栈溢出的资料相对…