大聪明教你学Java | Spring Boot 整合 Redis 实现访问量统计

大聪明教你学Java | Spring Boot 整合 Redis 实现访问量统计,第1张

大聪明教你学Java | 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 依赖



	org.springframework.boot
	spring-boot-starter-data-redis

接下来就在配置文件中增加 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 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 hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    
    public boolean hmset(String key, Map map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    
    public boolean hmset(String key, Map 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 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 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 value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    
    public boolean lSet(String key, List 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 redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 1.创建 redisTemplate 模版
        RedisTemplate 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 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( ̄▽ ̄)ブ

小结

本人经验有限,有些地方可能讲的没有特别到位,如果您在阅读的时候想到了什么问题,欢迎在评论区留言,我们后续再一一探讨‍

希望各位小伙伴动动自己可爱的小手,来一波点赞+关注 (✿◡‿◡) 让更多小伙伴看到这篇文章~ 蟹蟹呦(●’◡’●)

如果文章中有错误,欢迎大家留言指正;若您有更好、更独到的理解,欢迎您在留言区留下您的宝贵想法。

你在被打击时,记起你的珍贵,抵抗恶意;
你在迷茫时,坚信你的珍贵,抛开蜚语;
爱你所爱 行你所行 听从你心 无问东西

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5671402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)