springboot redis 统计
来源:互联网 发布:中兴网络机顶盒设置 编辑:程序博客网 时间:2024/06/08 12:22
package com.xu.boot;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.concurrent.TimeUnit;/** * Created by Administrator on 2017/9/4. */@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan@RestControllerpublic class RedisTest { public String key = "apiCount"; public boolean flag = false; public static void main(String[] args) { SpringApplication.run(RedisTest.class, args); }// @Autowired RedisTemplate<Object, Object > redisTemplate; @Autowired(required = false) public void setRedisTemplate(RedisTemplate redisTemplate) { RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); this.redisTemplate = redisTemplate; } @RequestMapping("/api") public void api() { if(this.flag) {// long count = redisTemplate.opsForValue().increment(key, 1);// if (count == 1) {// //设置有效期一分钟// redisTemplate.expire(key, 60, TimeUnit.SECONDS);// }// redisTemplate.opsForValue().increment(key, 1); long time = System.currentTimeMillis(); redisTemplate.opsForZSet().add(key, "url" + time, time); } } @RequestMapping("/show") @ResponseBody public Long show() throws Exception {// String str = (String) this.redisTemplate.opsForValue().get(key); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = new GregorianCalendar(); Date enddate = new Date(); System.out.println("系统当前时间:"+df.format(enddate)); c.setTime(enddate);//设置参数时间 c.add(Calendar.SECOND,-60);//把日期往后增加SECOND 秒.整数往后推,负数往前移动 Date startdate=c.getTime(); //这个时间就是日期往后推一天的结果 String str = df.format(startdate); System.out.println("系统前30秒时间:"+str); Long count = this.redisTemplate.opsForZSet().count(key, startdate.getTime(), enddate.getTime()); return count; } @RequestMapping("/start") public String start() throws Exception { this.flag = true; return "start"; } @RequestMapping("/stop") public String stop() throws Exception { this.flag = false; return "stop"; } @RequestMapping("/reset") public String reset() throws Exception {// redisTemplate.opsForValue().getAndSet(key, "0");// this.redisTemplate.opsForZSet().remove(key, "url*"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = new GregorianCalendar(); Date enddate = new Date(); System.out.println("系统当前时间:"+df.format(enddate)); this.redisTemplate.opsForZSet().removeRangeByScore(key, 0, enddate.getTime()); return "reset"; }}
阅读全文
0 0
- springboot redis 统计
- springboot redis
- SpringBoot-Redis
- SpringBoot-Redis
- SpringBoot + Redis
- springboot--redis
- redis统计
- Redis---------springboot整合Redis实例
- SpringBoot中使用redis
- SpringBoot -- 集成Redis/CacheManager
- springboot+redis作缓存
- SpringBoot学习:整合Redis
- Springboot整合Redis
- SpringBoot+Redis集群
- SpringBoot -- 集成Redis/CacheManager
- SpringBoot之redis
- springboot整合redis缓存
- Springboot中redis
- python库paramiko,ConfigParser实践
- 解析soap Xml
- RegExp概述--常用正则表达式大全
- C++ Builder打开VCL控件的双缓冲
- 基于Fast Bilateral Filtering 算法的 High-Dynamic Range(HDR) 图像显示技术。
- springboot redis 统计
- Eclipse设置相关整理
- 基于ZXing二维码实现(示例三)
- 三角测距法的原理
- 1常量
- android机顶盒真机调试方法
- sap使用DB13和brtools发起oracle数据库备份
- Jfinal事务
- Oracle密码复杂度设置