springboot redis 统计

这是一个关于如何使用SpringBoot结合Redis实现API访问统计的示例。代码中定义了一个RedisTest类,通过@RestController注解使其成为RESTful控制器。主要方法包括/api用于增加访问计数,/show用于展示过去30秒的访问次数,/start和/stop控制统计开关,/reset清空统计。使用RedisTemplate操作Redis,设置key-value以及zset进行时间戳相关的计数管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
@RestController
public 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";

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值