Giter VIP home page Giter VIP logo

redis-tool's Introduction

如何使用?

1、对象缓存

注入redis工具类

  @Autowired
  private RedisUtil redisUtil;

构建缓存key前缀,PrefixKey.with(int expireSeconds, String prefix) ,参数1:过期时间(单位:秒),参数2:key前缀

    PrefixKey pk = PrefixKey.with(3600, "jyx");

添加对象缓存,set(KeyPrefix prefix, String key, T value, boolean isRandomSeconds) ,参数1:前缀对象,参数2:缓存key,参数3:要缓存的对象 参数3:是否开启避免雪崩(会产生过期时间小误差)

   redisUtil.set(pk, "q", user1, true);

从缓存获取对象,get(KeyPrefix prefix, String key, Class clazz),参数1:前缀对象,参数2:缓存key,参数3:对象类型

  User user = redisUtil.get(pk, "q", User.class);

2、分布式锁

注入redis工具类

  @Autowired
  private RedisUtil redisUtil;

加锁和释放锁,lock(String lockKey), 参数:标记锁,保证锁唯一性

 try {
      redisUtil.lock("insertlock");    
     }finally {
      redisUtil.unlock("insertlock");  
   }

3、限流

通过添加注解 @RedisLimit(seconds, maxCount)实现请求的限流,参数1:时间(单位:秒),参数2:最大请求数

    @RedisLimit(seconds = 5,maxCount = 5)//5秒内最多能请求5次
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Result<Integer> list(Model model, User user, @RequestParam("goodsId") long goodsId) {
        ...
        return Result.success(0);//排队中

    }

redis-tool's People

Contributors

zaiyunduan123 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.