redis安装注意事项
1获取reids资源并且解压
2 打开cmd窗口 进入到redis目录
3 输入启动命令 redis-server.exe redis.windows.conf
java集成:pom文件
1 2 3 4 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> |
配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | spring.redis.host= 127.0 . 0.1 spring.redis.port= 6379 spring.redis.password= spring.redis.lettuce.pool.max-active= 20 spring.redis.lettuce.pool.max-wait=- 1 spring.redis.lettuce.pool.max-idle= 5 spring.redis.lettuce.pool.min-idle= 1 spring.redis.timeout= 5000 |
加载配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Configuration public class RedisConfig extends CachingConfigurerSupport{ @Bean (name= "redisTemplate" ) public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) { StringRedisTemplate template = new StringRedisTemplate(connectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object. class ); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); System.out.println( "--------------------------" +template.toString()); return template; } } |
工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | @Component public class RedisUtil { protected RedisTemplate<String, Object> redisTemplate; @Resource public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) { this .redisTemplate = redisTemplate; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set( final String key, Object value, long timeOut) { boolean result = false ; try { ValueOperations<String, Object> operations = redisTemplate.opsForValue(); operations.set(key, value, timeOut, TimeUnit.SECONDS); result = true ; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set( final String key, Object value) { boolean result = false ; try { ValueOperations<String, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true ; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 读取缓存 * * @param key * @return */ public Object get( final String key) { Object result = null ; try { ValueOperations<String, Object> operations = redisTemplate.opsForValue(); if (!exists(key)) { return null ; } result = operations.get(key); return result; } catch (Exception e) { e.printStackTrace(); return null ; } } /** * 删除缓存 */ public void remove( final String key) { redisTemplate.delete(key); } public boolean exists(String key) { return redisTemplate.hasKey(key); } public Long createIncrement(String key, int value){ ValueOperations<String, Object> operations = redisTemplate.opsForValue(); return operations.increment(key, value); } public void expire(String key, int coloseSeconds){ redisTemplate.expire(key,coloseSeconds,TimeUnit.SECONDS); } /*添加当天新增用户数量*/ public void newIncrement() { ValueOperations<String, Object> operations = redisTemplate.opsForValue(); operations.increment( "new" , 1 ); } /*添加当天活跃用户数量*/ public void activeIncrement() { ValueOperations<String, Object> operations = redisTemplate.opsForValue(); operations.increment( "active" , 1 ); } /*获取当天新增用户数量*/ public Integer getNewCount() { return Integer.valueOf(redisTemplate.boundValueOps( "new" ).get( 0 , - 1 )); } /*获取当天活跃用户数量*/ public Integer getActiveCount() { return Integer.valueOf(redisTemplate.boundValueOps( "active" ).get( 0 , - 1 )); } //日活 public boolean setBit(String key ,Long offset , boolean b){ return redisTemplate.opsForValue().setBit(key,offset,b); } //日活 public boolean getBit(String key,Long offset){ return redisTemplate.opsForValue().getBit(key,offset); } } |
测试类Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Resource RedisUtil redisUtil; @RequestMapping ( "login" ) public String loginCount(){ for ( long i = 0 ; i< 2000000 ; i++){ if (i% 2 == 0 ){ redisUtil.setBit( "login20220317" ,i, true ); } } return "ok" ; } @RequestMapping ( "getLogin" ) public String getLogin(){ for ( long i = 0 ; i< 1000000 ; i++){ System.out.println( "{" +redisUtil.getBit( "login20220317" ,i)+ "}" ); } return "ok" ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗