近期在整合springboot + redis 的功能,本来想用原生的jedit api,最后想想有点 low,搜了一把,boot已经提供给我们操作的方法,那就是

使用 redisTemplate 或 StringRedisTemplate, 两者是有区别的,可以看下面的说明

1. 两者的关系是StringRedisTemplate继承RedisTemplate。

2. 两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。

3. SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。

StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。

RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

引自: https://blog.csdn.net/yifanSJ/article/details/79513179

好了,有关概念的解释不在此处详细说明,这里只是记录如何快速搭建和实现操作redis,先看下我的工程结构,如图:

引入相关jar包,pom.xml如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-data-redis</artifactId>
  9. </dependency>
    <!-- 因为需要使用lettuce连接池,这个包必须添加 -->
  10. <dependency>
  11. <groupId>org.apache.commons</groupId>
  12. <artifactId>commons-pool2</artifactId>
  13. <version>2.6.1</version><!--$NO-MVN-MAN-VER$-->
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-cache</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.fasterxml.jackson.core</groupId>
  21. <artifactId>jackson-core</artifactId>
  22. <version>2.9.8</version><!--$NO-MVN-MAN-VER$-->
  23. </dependency>
  24. <dependency>
  25. <groupId>com.fasterxml.jackson.core</groupId>
  26. <artifactId>jackson-annotations</artifactId>
  27. <version>2.9.8</version><!--$NO-MVN-MAN-VER$-->
  28. </dependency>
  29. <dependency>
  30. <groupId>com.fasterxml.jackson.core</groupId>
  31. <artifactId>jackson-databind</artifactId>
  32. <version>2.9.8</version><!--$NO-MVN-MAN-VER$-->
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. <version>1.18.2</version><!--$NO-MVN-MAN-VER$-->
  38. </dependency>
  39.         <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version><!--$NO-MVN-MAN-VER$-->
            </dependency>
  40. </dependencies>

配置Redis参数 application.properties:

  1. # 配置redis参数
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器连接密码(默认为空)
    spring.redis.password=
    # Redis服务器地址
    spring.redis.host=127.0.0.1
    # Redis服务器连接端口
    spring.redis.port=6379
    # 连接超时时间,单位(毫秒)
    spring.redis.timeout=5000
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.jedis.pool.max-active=200
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.jedis.pool.max-wait=20000
    # 连接池中的最大空闲连接
    spring.redis.jedis.pool.max-idle=10
    # 连接池中的最小空闲连接
    spring.redis.jedis.pool.min-idle=10
    # 集群
    #spring.redis.cluster.nodes=192.168.211.134:7000,192.168.211.134:7001,192.168.211.134:7002
    #spring.redis.cluster.max-redirects=6

创建RedisConfiguration类:

  1. package com.szl.demo.common.redisConfig;
  2.  
  3. import org.springframework.cache.annotation.CachingConfigurerSupport;
  4. import org.springframework.cache.annotation.EnableCaching;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.RedisSerializer;
  10. import org.springframework.data.redis.serializer.StringRedisSerializer;
  11. import com.fasterxml.jackson.annotation.PropertyAccessor;
  12. import com.fasterxml.jackson.databind.ObjectMapper;
  13. import com.fasterxml.jackson.annotation.*;
  14.  
  15. @EnableCaching
  16. @Configuration
  17. public class RedisConfiguration extends CachingConfigurerSupport {
  18.  
  19. /**
  20. * @param connectionFactory
  21. * @return
  22. * @desc redis模板,存储关键字是字符串,
  23. * 值jackson2JsonRedisSerializer是序列化后的值
  24. */
    @Bean
  25. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  26. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  27. redisTemplate.setConnectionFactory(connectionFactory);
  28. // 开启事务
  29. redisTemplate.setEnableTransactionSupport(true);
  30.  
  31. // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
  32. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
  33. new Jackson2JsonRedisSerializer<>(Object.class);
  34. ObjectMapper objectMapper = new ObjectMapper();
  35. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  36. objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  37. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  38.  
  39. // 使用StringRedisSerializer来序列化和反序列化redis的key值
  40. RedisSerializer<?> redisSerializer = new StringRedisSerializer();
  41. // key
  42. redisTemplate.setKeySerializer(redisSerializer);
  43. redisTemplate.setHashKeySerializer(redisSerializer);
  44. // value
  45. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  46. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  47.  
  48. redisTemplate.afterPropertiesSet();
  49. return redisTemplate;
  50. }
  51.  
  52. }

DTO 类:

  1. package com.szl.demo.common.dto;
  2.  
  3. import java.io.Serializable;
  4. import lombok.Data;
  5.  
  6. @Data
  7. public class UserDto implements Serializable {
  8. private static final long serialVersionUID = -8858511759866491158L;
  9.  
  10. private String userName;
  11. private Integer userAge;
  12.  
  13. }

UserService接口:

  1. package com.szl.demo.service;
  2.  
  3. import com.szl.demo.common.dto.UserDto;
  4.  
  5. public interface UserService {
  6.  
  7. /**
  8. * @param userDto
  9. * @desc 将字符串保存到redis中
  10. */
  11. public void saveUserInfoToRedis();
  12.  
  13. /**
  14. * @param key
  15. * @return
  16. * @desc 从redis中读取字符串
  17. */
  18. public String getUserInfoFromRedis(String key);
  19.  
  20. /**
  21. * @param userDto
  22. * @desc 将对象保存到redis中
  23. */
  24. public void saveUserObject(UserDto userDto);
  25.  
  26. /**
  27. * @param userName
  28. * @return
  29. * @desc 从redis中获取对象
  30. */
  31. public UserDto findUserObject(String userName);
  32.  
  33. /**
  34. * @param userDto
  35. * @desc 锁机制保存对象数据
  36. */
  37. public void lockOfUserProcess(UserDto userDto);
  38.  
  39. }

UserServiceImpl实现接口:

  1. package com.szl.demo.service.impl;
  2.  
  3. import java.util.concurrent.TimeUnit;
  4. import javax.annotation.Resource;
  5. import org.springframework.beans.BeanUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.stereotype.Service;
  9. import com.szl.demo.common.dto.UserDto;
  10. import com.szl.demo.common.redisConfig.RedisDistributedLock;
  11. import com.szl.demo.common.util.JsonWare;
  12. import com.szl.demo.service.UserService;
  13. import lombok.extern.slf4j.Slf4j;
  14.  
  15. @Slf4j
  16. @Service("userService")
  17. public class UserServiceImpl implements UserService {
  18. @Autowired
  19. private RedisTemplate<String, Object> redisTemplate;
  20. @Autowired
  21. private RedisDistributedLock redisDistributedLock;
  22.  
  23. /**
  24. * @param userDto
  25. * @desc 将字符串保存到redis中
  26. */
  27. public void saveUserInfoToRedis() {
  28. // 判断redis中是否存在key
  29. boolean isExist = redisTemplate.hasKey("demo_test02");
  30. if (!isExist) {
  31. // 保存key,有效期为30秒
    String msg = "abc123,你好,welcome.";
  32. redisTemplate.opsForValue().set("demo_test02", msg, 30, TimeUnit.SECONDS);
  33. } else {
  34. // 删除key
  35. redisTemplate.delete("demo哈哈");
  36. }
  37. }
  38.  
  39. /**
  40. * @param key
  41. * @return
  42. * @desc 从redis中读取字符串
  43. */
  44. public String getUserInfoFromRedis(String key) {
  45. String val = (String)redisTemplate.opsForValue().get(key);
  46. return val;
  47. }
  48.  
  49. /**
  50. * @param userDto
  51. * @desc 将对象保存到redis中
  52. */
  53. public void saveUserObject(UserDto userDto) {
  54. // 判断redis中是否存在key
  55. boolean isExist = redisTemplate.hasKey(userDto.getUserName());
  56. if (!isExist) {
  57. // 保存key,有效期为30秒
  58. redisTemplate.opsForValue().set(userDto.getUserName(), userDto, 30, TimeUnit.SECONDS);
  59. } else {
  60. // 删除key
  61. redisTemplate.delete(userDto.getUserName());
  62. }
  63. }
  64.  
  65. /**
  66. * @param userName
  67. * @return
  68. * @desc 从redis中获取对象
  69. */
  70. public UserDto findUserObject(String userName) {
  71. UserDto userDto = (UserDto) redisTemplate.opsForValue().get(userName);
  72. return userDto;
  73. }
  74.  
  75. /**
  76. * @param userDto
  77. * @desc 锁机制保存对象数据
  78. */
  79. public void lockOfUserProcess(UserDto userDto) {
  80. String key = "myLock_" + userDto.getUserName();
  81. int timeout = 300 * 1000;//超时时间 5分钟
  82. long value = System.currentTimeMillis() + timeout;
  83.  
  84. try {
  85. // 加锁
  86. if (!redisDistributedLock.setLock(key, String.valueOf(value))) {
  87. throw new Exception("对不起,redis被挤爆了,请休息片刻再重试。");
  88. }
  89. // 做一些业务相关操作,这里只是demo,随便保存个对象信息
  90. redisTemplate.opsForValue().set(userDto.getUserName(), userDto, 60, TimeUnit.SECONDS);
  91. log.info("原来值内容:" + JsonWare.beanToJson(userDto));
  92. // 修改值,重新保存到redis中
  93. UserDto dto = new UserDto();
  94. BeanUtils.copyProperties(userDto, dto);
  95. dto.setUserAge(30);
  96. redisTemplate.opsForValue().set(dto.getUserName(), dto, 60, TimeUnit.SECONDS);
  97. log.info("修改值内容:" + JsonWare.beanToJson(dto));
  98. } catch (Exception e) {
  99. log.error("异常发生,信息如下:", e.getMessage());
  100. } finally {
  101. // 释放锁
  102. redisDistributedLock.releaseLock(key, String.valueOf(value));
  103. }
  104. }
  105. }

Controller类:

  1. package com.szl.demo.controller;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.ModelMap;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import com.szl.demo.common.dto.UserDto;
  12. import com.szl.demo.service.UserService;
  13.  
  14. @Controller
  15. public class DemoController {
  16. @Autowired
  17. private UserService userService;
  18.  
  19. @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
  20. public void saveUser(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
  21. userService.saveUserInfoToRedis();
  22. }
  23.  
  24. @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
  25. public void getUserInfo(HttpServletRequest request, HttpServletResponse response,
  26. @RequestParam(value = "key", required = false) String key) {
  27. String msg = userService.getUserInfoFromRedis(key);
  28. System.out.println(msg);
  29. }
  30.  
  31. @RequestMapping(value = "/saveUserObject", method = RequestMethod.POST)
  32. public void saveUserObject(HttpServletRequest request, HttpServletResponse response) {
  33. UserDto dto = new UserDto();
  34. dto.setUserName("Jimmy Shan");
  35. dto.setUserAge(21);
  36. userService.saveUserObject(dto);
  37. }
  38.  
  39. @RequestMapping(value = "/getUserObject", method = RequestMethod.GET)
  40. public void getUserObject(HttpServletRequest request, HttpServletResponse response,
  41. @RequestParam(value = "key", required = false) String key) {
  42. UserDto dto = userService.findUserObject(key);
  43. System.out.println("姓名: " + dto.getUserName() + ", 年龄: " + dto.getUserAge());
  44. }
  45.  
  46. @RequestMapping(value = "/lockDealWithDemo", method = RequestMethod.GET)
  47. public void lockDealWithDemo(HttpServletRequest request, HttpServletResponse response) {
  48. UserDto dto = new UserDto();
  49. dto.setUserName("JimmyShan");
  50. dto.setUserAge(16);
  51. userService.lockOfUserProcess(dto);
  52. System.out.println("这是lock的demo请求");
  53. }
  54.  
  55. }

RedisDistributedLock类(锁的工具类) :

  1. package com.szl.demo.common.redisConfig;
  2.  
  3. import javax.annotation.Resource;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.util.StringUtils;
  7. import lombok.extern.slf4j.Slf4j;
  8.  
  9. /**
  10. * @author Jimmy Shan
  11. * @desc Redis 锁工具类
  12. */
  13. @Slf4j
  14. @Component
  15. public class RedisDistributedLock {
  16. @Resource
  17. private RedisTemplate<String, Object> redisTemplate;
  18.  
  19. /**
  20. * @param key redis key, 唯一键
  21. * @param value redis value, 这里是时间戳
  22. * @return
  23. * @desc 加锁 true已锁 false未锁
  24. */
  25. public boolean setLock(String key, String value) {
  26. if(redisTemplate.opsForValue().setIfAbsent(key, value)) { // 对应setnx命令
  27. //可以成功设置,也就是key不存在
  28. return true;
  29. }
  30. // 判断锁超时 - 防止原来的操作异常,没有运行解锁操作 防止死锁
  31. String currentValue = (String) redisTemplate.opsForValue().get(key);
  32. // 如果锁过期
  33. // currentValue 不为空且小于当前时间
  34. if(!StringUtils.isEmpty(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()) {
  35. // 获取上一个锁的时间value
  36. // 对应getset,如果key存在返回当前key的值,并重新设置新的值
  37. // redis是单线程处理,即使并发存在,这里的getAndSet也是单个执行
  38. // 所以,加上下面的 !StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)
  39. // 就能轻松解决并发问题
  40. String oldValue = (String) redisTemplate.opsForValue().getAndSet(key,value);
  41. if(!StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47.  
  48. /**
  49. * @param key redis key, 唯一键
  50. * @param value redis value, 这里是时间戳
  51. * @return
  52. * @desc 释放锁 true已释放 false未释放
  53. */
  54. public void releaseLock(String key, String value) {
  55. try {
  56. String currentValue = (String) redisTemplate.opsForValue().get(key);
  57. if(!StringUtils.isEmpty(currentValue) && currentValue.equals(value)) {
  58. redisTemplate.opsForValue().getOperations().delete(key);// 删除key
  59. }
  60. } catch (Exception e) {
  61. log.error("解锁出现异常了,{}", e);
  62. }
  63. }
  64. }

我们去控制台看下效果

现在我们能看到,value为 "abc123,你好,welcome." 中的 中文字体已经被序列化了, 还有 UserDto对象,是以 json格式存储。

以上属于直连模式,这种方式在访问量不高的时候,足够应付,游刃有余,反之,该如何处理呢 ?

答案是:连接池

下面是如何使用连接池的方法,以上的代码项保持不变,只要修改 “ RedisConfiguration ” 这个类即可,看下面具体实现

使用连接池的 RedisConfiguration 类:

  1. package com.szl.demo.common.redisConfig;
  2.  
  3. import java.time.Duration;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cache.annotation.CachingConfigurerSupport;
  6. import org.springframework.cache.annotation.EnableCaching;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.core.env.Environment;
  10. import org.springframework.data.redis.connection.RedisConnectionFactory;
  11. import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
  12. import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
  13. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  14. import org.springframework.data.redis.core.RedisTemplate;
  15. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  16. import org.springframework.data.redis.serializer.RedisSerializer;
  17. import org.springframework.data.redis.serializer.StringRedisSerializer;
  18. import com.fasterxml.jackson.annotation.PropertyAccessor;
  19. import com.fasterxml.jackson.databind.ObjectMapper;
  20. import redis.clients.jedis.JedisPoolConfig;
  21. import com.fasterxml.jackson.annotation.*;
  22.  
  23. @EnableCaching
  24. @Configuration
  25. public class RedisConfiguration extends CachingConfigurerSupport {
  26. @Autowired
  27. private Environment env;
  28.  
  29. /**
  30. * @param connectionFactory
  31. * @return
  32. * @desc redis模板,存储关键字是字符串,
  33. * 值jackson2JsonRedisSerializer是序列化后的值
  34. */
  35. @Bean
  36. public RedisTemplate<String, Object> redisTemplate() {
  37. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  38. redisTemplate.setConnectionFactory(connectionPoolsFactory());
  39. // 开启事务
  40. //redisTemplate.setEnableTransactionSupport(true);
  41.  
  42. // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
  43. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
  44. new Jackson2JsonRedisSerializer<>(Object.class);
  45. ObjectMapper objectMapper = new ObjectMapper();
  46. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  47. objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  48. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  49.  
  50. // 使用StringRedisSerializer来序列化和反序列化redis的key值
  51. RedisSerializer<?> redisSerializer = new StringRedisSerializer();
  52. // key
  53. redisTemplate.setKeySerializer(redisSerializer);
  54. redisTemplate.setHashKeySerializer(redisSerializer);
  55. // value
  56. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  57. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  58.  
  59. redisTemplate.afterPropertiesSet();
  60. return redisTemplate;
  61. }
  62.  
  63. /**
  64. * @desc 使用jedis pool创建连接(连接池配置)
  65. */
  66. private RedisConnectionFactory connectionPoolsFactory() {
  67. JedisPoolConfig poolConfig = new JedisPoolConfig();
  68. // 最大空闲连接数, 默认8个
  69. poolConfig.setMaxIdle(Integer.parseInt(env.getProperty("spring.redis.jedis.pool.max-idle")));
  70. // 最小空闲连接数, 默认0
  71. poolConfig.setMinIdle(Integer.parseInt(env.getProperty("spring.redis.jedis.pool.min-idle")));
  72. // 最大连接数, 默认8个
  73. poolConfig.setMaxTotal(Integer.parseInt(env.getProperty("spring.redis.jedis.pool.max-active")));
  74. // 获取连接时的最大等待毫秒数, 如果不超时设置: -1
  75. poolConfig.setMaxWaitMillis(Long.parseLong(env.getProperty("spring.redis.jedis.pool.max-wait")));
  76. // 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
  77. poolConfig.setTimeBetweenEvictionRunsMillis(-1);
  78. // 在获取连接的时候检查有效性, 默认false
  79. poolConfig.setTestOnBorrow(true);
  80. // 在空闲时检查有效性, 默认false
  81. poolConfig.setTestWhileIdle(true);
  82.  
  83. JedisClientConfiguration jedisClientConfiguration =
  84. JedisClientConfiguration.builder().usePooling().poolConfig(poolConfig).and()
  85. .readTimeout(Duration.ofMillis(Long.parseLong(env.getProperty("spring.redis.timeout"))))
  86. .connectTimeout(Duration.ofMillis(Long.parseLong(env.getProperty("spring.redis.timeout"))))
  87. .build();
  88. RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
  89. redisStandaloneConfiguration.setDatabase(Integer.parseInt(env.getProperty("spring.redis.database")));
  90. redisStandaloneConfiguration.setHostName(env.getProperty("spring.redis.host"));
  91. redisStandaloneConfiguration.setPassword(env.getProperty("spring.redis.password"));
  92. redisStandaloneConfiguration.setPort(Integer.parseInt(env.getProperty("spring.redis.port")));
  93. return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
  94. }
  95.  
  96. }

至此,我们就跑起来看效果了,以上是本人经过测试并通过的代码和配置,另外需要说明一点,redis服务器本人使用3.0.5, 之前在使用2.4.5的时候,总是连接死锁(win环境),折腾了许久,

最后还是更新高版本解决问题。

如有朋友参考本人的笔记,有问题可以留言,转载请注明原著,谢谢。

锁来源参考:https://blog.csdn.net/qq_26525215/article/details/79182687

springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5的更多相关文章

  1. 使用Spring操作Redis的key-value数据

    前言 最近工作一直忙的不可开交,小Alan已经很久没有和大家分享知识了,在深圳待了两年多,依然感觉自己还是个小菜鸟,工作中还是会遇到很多自己在短期内无法搞定的事情,每当这个时候总是会感觉到很沮丧,就会 ...

  2. 【快学springboot】13.操作redis之String数据结构

    前言 在之前的文章中,讲解了使用redis解决集群环境session共享的问题[快学springboot]11.整合redis实现session共享,这里已经引入了redis相关的依赖,并且通过spr ...

  3. 使用RedisTemplate的操作类访问Redis(转)

    深入理解Spring Redis的使用 (三).使用RedisTemplate的操作类访问Redis 事务需要开启enableTransactionSupport,然后使用@transactional ...

  4. Spring中使用RedisTemplate操作Redis(spring-data-redis)

    RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...

  5. springboot之使用redistemplate优雅地操作redis

    概述 本文内容主要 关于spring-redis 关于redis的key设计 redis的基本数据结构 介绍redis与springboot的整合 sringboot中的redistemplate的使 ...

  6. SpringBoot 使用RedisTemplate操作Redis

    新版: import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.T ...

  7. spring-data-redis 中使用RedisTemplate操作Redis

    Redis 数据结构简介 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集合 ...

  8. RedisTemplate操作Redis

    RedisTemplate Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序 ...

  9. Java 使用Jedis和RedisTemplate操作Redis缓存(SpringBoot)

    package com.example.redis.controller; import com.example.redis.entity.User; import com.example.redis ...

随机推荐

  1. openresty开发系列37--nginx-lua-redis实现访问频率控制

    openresty开发系列37--nginx-lua-redis实现访问频率控制 一)需求背景 在高并发场景下为了防止某个访问ip访问的频率过高,有时候会需要控制用户的访问频次在openresty中, ...

  2. VS Code中配置python版本以及Python多版本

    VS Code中配置python版本VS Code十分方便配置python的版本:可以选在在本地setting.json或者全局setting.json文件中配置:python.pythonPath在 ...

  3. 使用EF 4.1的DbContext的方法大全

    简述:EF4.1包括Code First和DbContext API.DbContext API为EF提供更多的工作方式:Code First,Database First和Model First. ...

  4. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  5. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. 【Python学习之二】Python基础语法

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.Python的注释及乱码1.单行注释:以#开头 ...

  7. postman上传文件对参数的contentType类型设置方式

    项目中使用postman模拟上传文件接口时,总是不成功,发现content-type设置不对,设置head的contentType后,还是不行,后来无意中发现文件参数默认的content-type类型 ...

  8. java email发送(附件中文的处理)

    这里使用的是commons-email-1.3.2.jar进行的开发,自认为这是简单的邮件发送. package com.yt.base.common; import java.io.Unsuppor ...

  9. Java开发笔记(一百零六)Fork+Join框架实现分而治之

    前面依次介绍了普通线程池和定时器线程池的用法,这两种线程池有个共同点,就是线程池的内部线程之间并无什么关联,然而某些情况下的各线程间存在着前因后果关系.譬如人口普查工作,大家都知道我国总人口为14亿左 ...

  10. python学习-37 其他的文件处理方法

    f = open('test.txt','r+',encoding='utf-8') f.flush() # 刷新 f.readline() print(f.tell()) # 说明光标位置在哪里 ( ...