信息发布→ 登录 注册 退出

SpringBoot整合Redis实现访问量统计的示例代码

发布时间:2026-01-11

点击量:
目录
  • 前言
  • Spring Boot 整合 Redis
    • 引入依赖、增加配置
  • 翠花!上代码

    前言

    之前开发系统的时候客户提到了一个需求:需要统计某些页面的访问量,记得当时还纠结了一阵子,不知道怎么去实现这个功能,后来还是在大佬的带领下借助 Redis 实现了这个功能。今天又回想起了这件事,正好和大家分享一下 Spring Boot 整合 Redis 实现访问量统计的全过程。

    首先先解释一下为什么需要借助 Redis,其实原因也很简单,就是因为它非常快(每秒可执行大约110000次的 SET 操作,每秒大约可执行81000次的 GET 操作),我们就可以把访问量暂存在 Redis 中,当有人访问页面的时候,就直接在 Redis 中执行 +1 的操作,然后再每隔一段时间把 Redis 中的访问量的数值写入到数据库中就搞定了~

    肯定有小伙伴会想:如果我们不借助 Redis 而是直接操作数据库的话会怎么样呢?

    访问量的统计是需要频繁读写的,如果不用 Redis 做缓存而是直接操作数据库的话,就会对数据库带来巨大的压力,试想一下如果此时有成千上万个人同时访问页面的话,数据库很可能在这一瞬间造成数据库的崩溃。对于这种高读写的场景,就需要直接在 Redis 上读写,等到合适的时间,再将数据批量写到数据库中。所以通常来说,在必要的时候引入Redis,可以减少MySQL(或其他)数据库的压力。

    Spring Boot 整合 Redis

    怎么创建 Spring Boot 项目这里就不提了,直接上重点——整合 Redis

    引入依赖、增加配置

    首先还是需要引入 Redis 依赖

    <!-- 集成Redis -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    接下来就在配置文件中增加 Redis 的相关配置

    # spring配置
    spring:
      # redis配置
      redis:
        host: 127.0.0.1
        port: 6379
        database: 0
        jedis:
          pool:
            max-active: 200
            max-idle: 500
            min-idle: 8
            max-wait: 10000
        timeout: 5000

    P.S. 如果 Redis 设置了密码,别忘了增加 password 配置哦 ~

    翠花!上代码

    首先在 Utils 包内新增一个 RedisUtil

    package com.media.common.utils;
    
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;
    
    import javax.annotation.Resource;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @program: media
     * @description: RedisUtil
     * @author: 庄霸.liziye
     * @create: 2025-12-15 10:02
     **/
    @Component
    public final class RedisUtil {
    
        @Resource
        private RedisTemplate<String, Object> redisTemplate;
    
        // =============================common============================
        /**
         * 指定缓存失效时间
         * @param key  键
         * @param time 时间(秒)
         */
        public boolean expire(String key, long time) {
            try {
                if (time > 0) {
                    redisTemplate.expire(key, time, TimeUnit.SECONDS);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 根据key 获取过期时间
         * @param key 键 不能为null
         * @return 时间(秒) 返回0代表为永久有效
         */
        public long getExpire(String key) {
            return redisTemplate.getExpire(key, TimeUnit.SECONDS);
        }
    
    
        /**
         * 判断key是否存在
         * @param key 键
         * @return true 存在 false不存在
         */
        public boolean hasKey(String key) {
            try {
                return redisTemplate.hasKey(key);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 删除缓存
         * @param key 可以传一个值 或多个
         */
        @SuppressWarnings("unchecked")
        public void del(String... key) {
            if (key != null && key.length > 0) {
                if (key.length == 1) {
                    redisTemplate.delete(key[0]);
                } else {
                    redisTemplate.delete(CollectionUtils.arrayToList(key));
                }
            }
        }
    
    
        // ============================String=============================
    
        /**
         * 普通缓存获取
         * @param key 键
         * @return 值
         */
        public Object get(String key) {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        }
    
        /**
         * 普通缓存放入
         * @param key   键
         * @param value 值
         * @return true成功 false失败
         */
    
        public boolean set(String key, Object value) {
            try {
                redisTemplate.opsForValue().set(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 普通缓存放入并设置时间
         * @param key   键
         * @param value 值
         * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
         * @return true成功 false 失败
         */
    
        public boolean set(String key, Object value, long time) {
            try {
                if (time > 0) {
                    redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
                } else {
                    set(key, value);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 递增
         * @param key   键
         * @param delta 要增加几(大于0)
         */
        public long incr(String key, long delta) {
            if (delta < 0) {
                throw new RuntimeException("递增因子必须大于0");
            }
            return redisTemplate.opsForValue().increment(key, delta);
        }
    
    
        /**
         * 递减
         * @param key   键
         * @param delta 要减少几(小于0)
         */
        public long decr(String key, long delta) {
            if (delta < 0) {
                throw new RuntimeException("递减因子必须大于0");
            }
            return redisTemplate.opsForValue().increment(key, -delta);
        }
    
    
        // ================================Map=================================
    
        /**
         * HashGet
         * @param key  键 不能为null
         * @param item 项 不能为null
         */
        public Object hget(String key, String item) {
            return redisTemplate.opsForHash().get(key, item);
        }
    
        /**
         * 获取hashKey对应的所有键值
         * @param key 键
         * @return 对应的多个键值
         */
        public Map<Object, Object> hmget(String key) {
            return redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * HashSet
         * @param key 键
         * @param map 对应多个键值
         */
        public boolean hmset(String key, Map<String, Object> map) {
            try {
                redisTemplate.opsForHash().putAll(key, map);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * HashSet 并设置时间
         * @param key  键
         * @param map  对应多个键值
         * @param time 时间(秒)
         * @return true成功 false失败
         */
        public boolean hmset(String key, Map<String, Object> map, long time) {
            try {
                redisTemplate.opsForHash().putAll(key, map);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 向一张hash表中放入数据,如果不存在将创建
         *
         * @param key   键
         * @param item  项
         * @param value 值
         * @return true 成功 false失败
         */
        public boolean hset(String key, String item, Object value) {
            try {
                redisTemplate.opsForHash().put(key, item, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 向一张hash表中放入数据,如果不存在将创建
         *
         * @param key   键
         * @param item  项
         * @param value 值
         * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
         * @return true 成功 false失败
         */
        public boolean hset(String key, String item, Object value, long time) {
            try {
                redisTemplate.opsForHash().put(key, item, value);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 删除hash表中的值
         *
         * @param key  键 不能为null
         * @param item 项 可以使多个 不能为null
         */
        public void hdel(String key, Object... item) {
            redisTemplate.opsForHash().delete(key, item);
        }
    
    
        /**
         * 判断hash表中是否有该项的值
         *
         * @param key  键 不能为null
         * @param item 项 不能为null
         * @return true 存在 false不存在
         */
        public boolean hHasKey(String key, String item) {
            return redisTemplate.opsForHash().hasKey(key, item);
        }
    
    
        /**
         * hash递增 如果不存在,就会创建一个 并把新增后的值返回
         *
         * @param key  键
         * @param item 项
         * @param by   要增加几(大于0)
         */
        public double hincr(String key, String item, double by) {
            return redisTemplate.opsForHash().increment(key, item, by);
        }
    
    
        /**
         * hash递减
         *
         * @param key  键
         * @param item 项
         * @param by   要减少记(小于0)
         */
        public double hdecr(String key, String item, double by) {
            return redisTemplate.opsForHash().increment(key, item, -by);
        }
    
    
        // ============================set=============================
    
        /**
         * 根据key获取Set中的所有值
         * @param key 键
         */
        public Set<Object> sGet(String key) {
            try {
                return redisTemplate.opsForSet().members(key);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    
        /**
         * 根据value从一个set中查询,是否存在
         *
         * @param key   键
         * @param value 值
         * @return true 存在 false不存在
         */
        public boolean sHasKey(String key, Object value) {
            try {
                return redisTemplate.opsForSet().isMember(key, value);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 将数据放入set缓存
         *
         * @param key    键
         * @param values 值 可以是多个
         * @return 成功个数
         */
        public long sSet(String key, Object... values) {
            try {
                return redisTemplate.opsForSet().add(key, values);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
    
        /**
         * 将set数据放入缓存
         *
         * @param key    键
         * @param time   时间(秒)
         * @param values 值 可以是多个
         * @return 成功个数
         */
        public long sSetAndTime(String key, long time, Object... values) {
            try {
                Long count = redisTemplate.opsForSet().add(key, values);
                if (time > 0) {
                    expire(key, time);
                }
                return count;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
    
        /**
         * 获取set缓存的长度
         *
         * @param key 键
         */
        public long sGetSetSize(String key) {
            try {
                return redisTemplate.opsForSet().size(key);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
    
        /**
         * 移除值为value的
         *
         * @param key    键
         * @param values 值 可以是多个
         * @return 移除的个数
         */
    
        public long setRemove(String key, Object... values) {
            try {
                Long count = redisTemplate.opsForSet().remove(key, values);
                return count;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
        // ===============================list=================================
    
        /**
         * 获取list缓存的内容
         *
         * @param key   键
         * @param start 开始
         * @param end   结束 0 到 -1代表所有值
         */
        public List<Object> lGet(String key, long start, long end) {
            try {
                return redisTemplate.opsForList().range(key, start, end);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    
        /**
         * 获取list缓存的长度
         *
         * @param key 键
         */
        public long lGetListSize(String key) {
            try {
                return redisTemplate.opsForList().size(key);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
    
        /**
         * 通过索引 获取list中的值
         *
         * @param key   键
         * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
         */
        public Object lGetIndex(String key, long index) {
            try {
                return redisTemplate.opsForList().index(key, index);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         */
        public boolean lSet(String key, Object value) {
            try {
                redisTemplate.opsForList().rightPush(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 将list放入缓存
         * @param key   键
         * @param value 值
         * @param time  时间(秒)
         */
        public boolean lSet(String key, Object value, long time) {
            try {
                redisTemplate.opsForList().rightPush(key, value);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
        }
    
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         * @return
         */
        public boolean lSet(String key, List<Object> value) {
            try {
                redisTemplate.opsForList().rightPushAll(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
        }
    
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         * @param time  时间(秒)
         * @return
         */
        public boolean lSet(String key, List<Object> value, long time) {
            try {
                redisTemplate.opsForList().rightPushAll(key, value);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 根据索引修改list中的某条数据
         *
         * @param key   键
         * @param index 索引
         * @param value 值
         * @return
         */
    
        public boolean lUpdateIndex(String key, long index, Object value) {
            try {
                redisTemplate.opsForList().set(key, index, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
        /**
         * 移除N个值为value
         *
         * @param key   键
         * @param count 移除多少个
         * @param value 值
         * @return 移除的个数
         */
    
        public long lRemove(String key, long count, Object value) {
            try {
                Long remove = redisTemplate.opsForList().remove(key, count, value);
                return remove;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
        
    }

    然后再新增一个 RedisConfig 类

    package com.media.common.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.data.redis.core.RedisOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.GenericToStringSerializer;
    
    
    /**
     * @program: media
     * @description: RedisConfiguration
     * @author: 庄霸.liziye
     * @create: 2025-12-15 10:16
     **/
    @Configuration
    @ConditionalOnClass(RedisOperations.class)
    @EnableConfigurationProperties(RedisProperties.class)
    public class RedisConfig {
    
        /**
         * 设置 redisTemplate 的序列化设置
         * @param redisConnectionFactory
         * @return
         */
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            // 1.创建 redisTemplate 模版
            RedisTemplate<Object, Object> template = new RedisTemplate<>();
            // 2.关联 redisConnectionFactory
            template.setConnectionFactory(redisConnectionFactory);
            // 3.创建 序列化类
            GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class);
            // 6.序列化类,对象映射设置
            // 7.设置 value 的转化格式和 key 的转化格式
            template.setValueSerializer(genericToStringSerializer);
            template.setKeySerializer(new StringRedisSerializer());
            template.afterPropertiesSet();
            return template;
        }
    }

    有些眼尖的小伙伴会发现在 RedisUtil 工具类中,我们在 private RedisTemplate<String, Object> redisTemplate 上增加的是 @Resource 注解,并非是 @Autowire 注解。

    原因也很简单,在源码中我们可以看到 RedisTemplate 指定的是泛型,如果在注入 RedisTemplate 时,值的部分使用了 Object ,那么再使用@AutoWired 注解注入就会报空指针的错误,所以需要使用 @Resource 注解(二者的区别是前者是根据类型注入后者是根据名字注入,具体的这里就不详细说,有兴趣的小伙伴可自行百度查阅)

    Redis 的相关代码到这里就写完了, 接下来我们就以“记录A页面的访问量”为需求,写一个简单的业务逻辑,代码仅供参考哦 ~

    首先我们新建一个数据库表,表结构很简单,只有三个字段,分别是ID、访问量、统计时间

    我们再写一下操作这个表的 CRUD 方法(这个也很简单,相信各位小伙伴都可以脑补出来 (●'◡'●) 所以在这里就不写具体代码了)

    此处略去一万个字....

    下面我们写一个监听类:

    package com.media.picture.handler;
    
    import com.media.common.utils.DateUtils;
    import com.media.common.utils.RedisUtil;
    import com.media.picture.domain.MamPictureView;
    import com.media.picture.service.IMamPictureViewService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    /**
     * @program: media
     * @description: ListenHandler
     * @author: 庄霸.liziye
     * @create: 2025-12-15 10:54
     **/
    @Component
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class ListenHandler {
    
        @Autowired
        private RedisUtil redisUtil;
    
        @Autowired
        private IMamPictureViewService iMamPictureViewService;
    
        public ListenHandler(){
            System.out.println("开始初始化");
        }
        
        @PostConstruct
        public void init() {
            System.out.println("Redis及数据库开始初始化");
            //插入一条空数据
            MamPictureView mamPictureView = new MamPictureView();
            mamPictureView.setViewNum(Long.valueOf(0));
            int viewId = iMamPictureViewService.insertMamPictureView(mamPictureView);
    
            redisUtil.set("pageA_id", viewId);
            redisUtil.set("pageA_count", 0);
            System.out.println("Redis及数据库初始化完毕");
        }
    }

    监听器的作用就是当项目启动后,在数据库表中插入一条空记录,并且在 Redis 中存入这条空记录的 id,并且将其访问量初始化为0。

    最后我们再写一下跳转A页面的方法:

    @Autowired
    private RedisUtil redisUtil;
    
    @GetMapping("/toPageA")
    public String toPageA()
    {
        redisUtil.incr("pageA_count",1);
        System.out.println("访问量:"+redisUtil.get("pageA_count"));
        return "/pageA";
    }

    这时候代码就全部搞定了,我们启动一下项目,看看执行效果

    我们每点跳转一次页面,Redis 中的访问量就会执行+1操作,实现了访问量的记录,最后一步就是把 Redis 中记录的访问量写入数据库就大功告成啦~

    我这里选择的是使用定时任务的方式写入,每间隔一段时间写入一次(为了能看到明显的效果,就写成了每间隔40秒执行一次)

    @Scheduled(cron = "*/40 * * * * ?")
    public void viewCount2DB(){
        System.out.println("准备从redis写入mysql");
        MamPictureView mamPictureView = new MamPictureView();
        mamPictureView.setViewId(Long.valueOf((String) redisUtil.get("pageA_id")));
        mamPictureView.setViewNum(Long.valueOf((String) redisUtil.get("pageA_count")));
        iMamPictureViewService.updateMamPictureView(mamPictureView);
        System.out.println("写入完毕");
    }

    P.S. 写入数据库的过程就很简单了,而且有很多办法可以实现写入的操作,这里的定时任务只作为参考哦~ o( ̄▽ ̄)ブ

    在线客服
    服务热线

    服务热线

    4008888355

    微信咨询
    二维码
    返回顶部
    ×二维码

    截屏,微信识别二维码

    打开微信

    微信号已复制,请打开微信添加咨询详情!