网站开发工程师证书,商务网站开发技术,网站续费查询,做网站怎样盈利文章目录 springboot中使用redisspringboot 连接 redis三种方式导入依赖增删改查小练习 springboot中使用redis
springboot 连接 redis三种方式
jedis #xff08;redis官方提供的#xff09;springboot自带的redisson (基于jedis优化的#xff0c;性能最好#xff0c;使… 文章目录 springboot中使用redisspringboot 连接 redis三种方式导入依赖增删改查小练习 springboot中使用redis
springboot 连接 redis三种方式
jedis redis官方提供的springboot自带的redisson (基于jedis优化的性能最好使用最多)
本文以springboot自带为例
导入依赖
导入springboot自带的redis依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencycontroller中注入即可。 Resourceprivate RedisTemplateString, String redisTemplate;增删改查
基本的增删改查很简单。 GetMapping(/test)public String test() {// 增redisTemplate.opsForValue().set(lpy, 123, 1, TimeUnit.HOURS);// 查String lpy redisTemplate.opsForValue().get(lpy);System.out.println(lpy); // 123// 改redisTemplate.opsForValue().set(lpy, 456, 1, TimeUnit.HOURS);lpy redisTemplate.opsForValue().get(lpy);System.out.println(lpy); // 456// 删redisTemplate.delete(lpy);lpy redisTemplate.opsForValue().get(lpy);System.out.println(lpy); // nullreturn success;}小练习
需求防止用户频繁访问10s内只能访问一次。 GetMapping(/access)public String access(String id) {String exit redisTemplate.opsForValue().get(user id);if (exit null || .equals(exit)) {redisTemplate.opsForValue().set(user id, true, 10, TimeUnit.SECONDS);return 可以访问;}return 访问过于频繁;}需求用户10s内只能访问5次。 GetMapping(/test2)public String test2(String id) {Long increment redisTemplate.opsForValue().increment(id, 1);redisTemplate.expire(id, 10, TimeUnit.SECONDS);if (increment 5) {return 你不能访问近10秒访问次数是 increment;} else {return 欢迎使用访问次数是 increment;}}需求发红包指定金额和数量保证数量 金额单位元。 抢红包每个红包最少为一元。利用list。 拆分的红包入队抢红包出队。
这里只是简单写的实际肯定是要记录发红包的人抢红包的人等等各种情况也不可能有这么多小数点。可以自行尝试。 这里主要是练习list的使用。 /*** 发红包* param money* param count* return*/GetMapping(/sendRedEnvelope)public String sendRedEnvelope(double money, int count) {if (count money) return 金额过小或个数过多请保证每个包最少可以有一元;Random rand new Random();double sum 0; // 检验一下是否分配正确for (int i 0; i count; i) {double max money - (count - i - 1) * 1; // 确保后面的钱够所以预留出足够的最小金额// 如果是最后一个包把剩余的钱全部放入if (i count - 1) {redisTemplate.opsForList().rightPush(redList, String.valueOf(max)); // 插入队列sum max;continue;}double redMoney rand.nextDouble() * (max - 1) 1.0; // 每个红包的金额money - redMoney; // 减去已经包好的红包金额redisTemplate.opsForList().rightPush(redList, String.valueOf(redMoney)); // 插入队列sum redMoney;}System.out.println(sum);return 发放成功;}/*** 抢红包* return*/GetMapping(/grabRedEnvelope)public String grabRedEnvelope() {Long redListSize redisTemplate.opsForList().size(redList);// 红包已经抢完if (redListSize 0) {return 此红包已经抢完;}// 获取红包double money Double.parseDouble(redisTemplate.opsForList().leftPop(redList));return 抢红包成功你抢了 money 元;}发红包
抢红包 文章转载自: http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.mmynk.cn.gov.cn.mmynk.cn http://www.morning.mkyny.cn.gov.cn.mkyny.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.xjkfb.cn.gov.cn.xjkfb.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.mypxm.com.gov.cn.mypxm.com