广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot整合Redis实现访问量统计的示例代码
  • 785
分享到

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

2024-04-02 19:04:59 785人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录前言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;


@Component
public final class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // =============================common============================
    
    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;
        }
    }

    
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }


    
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    @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=============================

    
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    

    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return 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;
        }
    }


    
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }


    
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }


    // ================================Map=================================

    
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    
    public Map<Object, Object> hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    
    public boolean hmset(String key, Map<String, Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return 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;
        }
    }


    
    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;
        }
    }

    
    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;
        }
    }


    
    public void hdel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }


    
    public boolean hHasKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }


    
    public double hincr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, by);
    }


    
    public double hdecr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, -by);
    }


    // ============================set=============================

    
    public Set<Object> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    
    public boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    
    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;
        }
    }


    
    public long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    

    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=================================

    
    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;
        }
    }


    
    public long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    
    public Object lGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    
    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    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;
        }

    }


    
    public boolean lSet(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    
    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;
        }
    }


    

    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;
        }
    }


    

    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;



@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {

    
    @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;


@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( ̄▽ ̄)ブ

到此这篇关于SpringBoot整合Redis实现访问量统计的示例代码的文章就介绍到这了,更多相关SpringBoot整合Redis访问量统计内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot整合Redis实现访问量统计的示例代码

本文链接: https://www.lsjlt.com/news/138597.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作