redis安装注意事项
1获取reids资源并且解压
2 打开cmd窗口 进入到redis目录
3 输入启动命令 redis-server.exe redis.windows.conf
java集成:pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置文件:
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
加载配置类
@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; } }
工具类
@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
@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"; }