博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
180626-Spring之借助Redis设计一个简单访问计数器
阅读量:6898 次
发布时间:2019-06-27

本文共 7360 字,大约阅读时间需要 24 分钟。

hot3.png

logo

文章链接:

Spring之借助Redis设计一个简单访问计数器

为什么要做一个访问计数?之前的个人博客用得是卜算子做站点访问计数,用起来挺好,但出现较多次的响应很慢,再其次就是个人博客实在是访问太少,数据不好看...

前面一篇博文简单介绍了Spring中的RedisTemplate的配置与使用,那么这篇算是一个简单的应用case了,主要基于Redis的计数器来实现统计

<!-- more -->

I. 设计

一个简单的访问计数器,主要利用redis的hash结构,对应的存储结构如下:

结构

存储结构比较简单,为了扩展,每个应用(or站点)对应一个APP,然后根据path路径进行分页统计,最后有一个特殊的用于统计全站的访问计数

II. 实现

主要就是利用Redis的hash结构,然后实现数据统计,并没有太多的难度,Spring环境下搭建redis环境可以参考:

1. Redis封装类

针对几个常用的做了简单的封装,直接使用RedisTemplate的excute方法进行的操作,当然也是可以使用 template.opsForValue() 等便捷方式,这里采用JSON方式进行对象的序列化和反序列化

public class QuickRedisClient {    private static final Charset CODE = Charset.forName("UTF-8");    private static RedisTemplate
template; public static void register(RedisTemplate
template) { QuickRedisClient.template = template; } public static void nullCheck(Object... args) { for (Object obj : args) { if (obj == null) { throw new IllegalArgumentException("redis argument can not be null!"); } } } public static byte[] toBytes(String key) { nullCheck(key); return key.getBytes(CODE); } public static byte[][] toBytes(List
keys) { byte[][] bytes = new byte[keys.size()][]; int index = 0; for (String key : keys) { bytes[index++] = toBytes(key); } return bytes; } public static String getStr(String key) { return template.execute((RedisCallback
) con -> { byte[] val = con.get(toBytes(key)); return val == null ? null : new String(val); }); } public static void putStr(String key, String value) { template.execute((RedisCallback
) con -> { con.set(toBytes(key), toBytes(value)); return null; }); } public static Long incr(String key, long add) { return template.execute((RedisCallback
) con -> { Long record = con.incrBy(toBytes(key), add); return record == null ? 0L : record; }); } public static Long hIncr(String key, String field, long add) { return template.execute((RedisCallback
) con -> { Long record = con.hIncrBy(toBytes(key), toBytes(field), add); return record == null ? 0L : record; }); } public static
T hGet(String key, String field, Class
clz) { return template.execute((RedisCallback
) con -> { byte[] records = con.hGet(toBytes(key), toBytes(field)); if (records == null) { return null; } return JSON.parseObject(records, clz); }); } public static
Map
hMGet(String key, List
fields, Class
clz) { List
list = template.execute((RedisCallback
>) con -> con.hMGet(toBytes(key), toBytes(fields))); if (CollectionUtils.isEmpty(list)) { return Collections.emptyMap(); } Map
result = new HashMap<>(); for (int i = 0; i < fields.size(); i++) { if (list.get(i) == null) { continue; } result.put(fields.get(i), JSON.parseObject(list.get(i), clz)); } return result; }}

对应的配置类

package com.git.hui.story.cache.redis;import com.git.hui.story.cache.redis.serializer.DefaultStrSerializer;import org.springframework.cache.CacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.RedisPassword;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;/** * Created by yihui in 18:45 18/6/11. */@Configuration@PropertySource(value = "classpath:application.yml")public class RedisConf {    private final Environment environment;    public RedisConf(Environment environment) {        this.environment = environment;    }    @Bean    public CacheManager cacheManager() {        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory()).build();    }    @Bean    public RedisTemplate
redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate
redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); DefaultStrSerializer serializer = new DefaultStrSerializer(); redisTemplate.setValueSerializer(serializer); redisTemplate.setHashValueSerializer(serializer); redisTemplate.setKeySerializer(serializer); redisTemplate.setHashKeySerializer(serializer); redisTemplate.afterPropertiesSet(); QuickRedisClient.register(redisTemplate); return redisTemplate; } @Bean public RedisConnectionFactory redisConnectionFactory() { LettuceConnectionFactory fac = new LettuceConnectionFactory(); fac.getStandaloneConfiguration().setHostName(environment.getProperty("spring.redis.host")); fac.getStandaloneConfiguration().setPort(Integer.parseInt(environment.getProperty("spring.redis.port"))); fac.getStandaloneConfiguration() .setPassword(RedisPassword.of(environment.getProperty("spring.redis.password"))); fac.afterPropertiesSet(); return fac; }}

2. Controller 支持

首先是定义请求参数:

@Datapublic class WebCountReqDO implements Serializable {    private String appKey;    private String referer;}

其次是实现Controller接口,稍稍注意下,根据path进行计数的逻辑:

  • 如果请求参数显示指定了referer参数,则用传入的参数进行统计
  • 如果没有显示指定referer,则根据header获取referer
  • 解析referer,分别对path和host进行统计+1,这样站点的统计计数就是根据host来的,而页面的统计计数则是根据path路径来的
@Slf4j@RestController@RequestMapping(path = "/count")public class WebCountController {    @RequestMapping(path = "cc", method = {RequestMethod.GET})    public ResponseWrapper
addCount(WebCountReqDO webCountReqDO) { String appKey = webCountReqDO.getAppKey(); if (StringUtils.isBlank(appKey)) { return ResponseWrapper.errorReturnMix(Status.StatusEnum.ILLEGAL_PARAMS_MIX, "请指定APPKEY!"); } String referer = ReqInfoContext.getReqInfo().getReferer(); if (StringUtils.isBlank(referer)) { referer = webCountReqDO.getReferer(); } if (StringUtils.isBlank(referer)) { return ResponseWrapper.errorReturnMix(Status.StatusEnum.FAIL_MIX, "无法获取请求referer!"); } return ResponseWrapper.successReturn(doUpdateCnt(appKey, referer)); } private CountDTO doUpdateCnt(String appKey, String referer) { try { if (!referer.startsWith("http")) { referer = "https://" + referer; } URI uri = new URI(referer); String host = uri.getHost(); String path = uri.getPath(); long count = QuickRedisClient.hIncr(appKey, path, 1); long total = QuickRedisClient.hIncr(appKey, host, 1); return new CountDTO(count, total); } catch (Exception e) { log.error("get referer path error! referer: {}, e: {}", referer, e); return new CountDTO(1L, 1L); } }}

3. 实例

针对这个简单的redis计数,目前在个人的mweb和zweb两个页面已经接入,在页脚处可以看到对应的计数,每次刷新计数会+1

  • mweb:
  • zweb:

III. 其他

0. 相关博文

1. :

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

2. 声明

尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

  • 微博地址:
  • QQ: 一灰灰/3302797840

3. 扫描关注

blogInfoV2.png

转载于:https://my.oschina.net/u/566591/blog/1836009

你可能感兴趣的文章
solr schema.xml配置详解
查看>>
yyModel字典(字典嵌套数组)转模型我遇到的坑
查看>>
密码嗅探工具dsniff
查看>>
一、数据库设计与性能优化--概述
查看>>
XamarinEssentials教程获取首选项的值
查看>>
Html的基础知识,基础标签的应用
查看>>
我的友情链接
查看>>
php获取客户端ip地址
查看>>
阴影覆盖scroll overflow
查看>>
php安装错误error: xml2-config not found. Please check your libxml2 installation
查看>>
Javascript中的checked
查看>>
我的友情链接
查看>>
mysql数据库启动失败
查看>>
ORA-14047
查看>>
phpMyadmin新建MYSQL数据库,添加用户并配置用户权限(图片教程)
查看>>
五、认识与学习BASH
查看>>
LVS之NAT模式的配置
查看>>
STP关键点总结
查看>>
echarts 图表设置
查看>>
linux 下heartbeat简单高可用集群搭建
查看>>