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

 更新时间:2018年06月27日 14:47:17   作者:一灰灰Blog  
本篇文章主要介绍了Spring之借助Redis设计一个简单访问计数器的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

I. 设计

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

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

II. 实现

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

Spring之RedisTemplate配置与使用

1. Redis封装类

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

public class QuickRedisClient {
  private static final Charset CODE = Charset.forName("UTF-8");
  private static RedisTemplate<String, String> template;

  public static void register(RedisTemplate<String, String> 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<String> 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<String>) 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<Void>) con -> {
      con.set(toBytes(key), toBytes(value));
      return null;
    });
  }

  public static Long incr(String key, long add) {
    return template.execute((RedisCallback<Long>) 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<Long>) con -> {
      Long record = con.hIncrBy(toBytes(key), toBytes(field), add);
      return record == null ? 0L : record;
    });
  }

  public static <T> T hGet(String key, String field, Class<T> clz) {
    return template.execute((RedisCallback<T>) con -> {
      byte[] records = con.hGet(toBytes(key), toBytes(field));
      if (records == null) {
        return null;
      }

      return JSON.parseObject(records, clz);
    });
  }

  public static <T> Map<String, T> hMGet(String key, List<String> fields, Class<T> clz) {
    List<byte[]> list =
        template.execute((RedisCallback<List<byte[]>>) con -> con.hMGet(toBytes(key), toBytes(fields)));
    if (CollectionUtils.isEmpty(list)) {
      return Collections.emptyMap();
    }

    Map<String, T> 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<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, String> 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 支持

首先是定义请求参数:

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

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

  1. 如果请求参数显示指定了referer参数,则用传入的参数进行统计
  2. 如果没有显示指定referer,则根据header获取referer
  3. 解析referer,分别对path和host进行统计+1,这样站点的统计计数就是根据host来的,而页面的统计计数则是根据path路径来的
@Slf4j
@RestController
@RequestMapping(path = "/count")
public class WebCountController {

  @RequestMapping(path = "cc", method = {RequestMethod.GET})
  public ResponseWrapper<CountDTO> 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);
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot中通过实现WebMvcConfigurer参数校验的方法示例

    SpringBoot中通过实现WebMvcConfigurer参数校验的方法示例

    这篇文章主要介绍了SpringBoot中通过实现WebMvcConfigurer参数校验的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Log4j 配置日志打印时区的实现方法

    Log4j 配置日志打印时区的实现方法

    下面小编就为大家分享一篇Log4j 配置日志打印时区的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • 带你了解Java的类和对象

    带你了解Java的类和对象

    下面小编就为大家带来一篇全面理解Java类和对象。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-09-09
  • java实现双人五子棋游戏

    java实现双人五子棋游戏

    这篇文章主要为大家详细介绍了java实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Java创建多线程的两种方式对比

    Java创建多线程的两种方式对比

    在Java中创建线程的方式有两种,第一种是直接继承Thead类,另一种是实现Runable接口。那么这两种方式孰优孰劣呢?这就是本文需要探讨的内容范畴了,看官们请仔细向下看
    2014-10-10
  • 详解springboot启动时是如何加载配置文件application.yml文件

    详解springboot启动时是如何加载配置文件application.yml文件

    这篇文章主要介绍了详解springboot启动时是如何加载配置文件application.yml文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Java基于二维数组实现的数独问题示例

    Java基于二维数组实现的数独问题示例

    这篇文章主要介绍了Java基于二维数组实现的数独问题,涉及java针对数组的遍历、计算、转换等相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • 鉴权认证+aop+注解+过滤feign请求的实例

    鉴权认证+aop+注解+过滤feign请求的实例

    这篇文章主要介绍了鉴权认证+aop+注解+过滤feign请求的实例讲解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 详解SpringBoot Redis自适应配置(Cluster Standalone Sentinel)

    详解SpringBoot Redis自适应配置(Cluster Standalone Sentinel)

    这篇文章主要介绍了详解SpringBoot Redis自适应配置(Cluster Standalone Sentinel),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • SWT(JFace)Group(分组显示)

    SWT(JFace)Group(分组显示)

    SWT(JFace)体验之Group(分组显示)
    2009-06-06

最新评论