当前位置: 首页 > news >正文

国外网站做盗版百度竞价返点开户

国外网站做盗版,百度竞价返点开户,网站做,做我女朋友网站问题: 先执行get获取值,判断符合条件再执行incr、decr操作。在临界缓存失效的情况下,会默认赋值当前key为永不过期的0,再执行加减法,导致程序异常。 推荐解决方案: 1、限制接口频率:先incr&…

问题
先执行get获取值,判断符合条件再执行incr、decr操作。在临界缓存失效的情况下,会默认赋值当前key为永不过期的0,再执行加减法,导致程序异常。

推荐解决方案
1、限制接口频率:先incr,执行后值为1,说明是第一次执行,需要额外设置过期时间,再判断是否超过当前接口频率限制(注意上述步骤不可调换顺序)

2、使用lua脚本完整提交一次操作,脚本中的key可以保证一致。以加减库存为例,先查询key存在的情况下,再进行库存变更,如果不存在无需处理,等待下次缓存加载即为最新的值


问题描述

场景1:我们缓存了一个商品的库存,过期时间为5分钟,根据用户的购买和取消执行 incr、decr 操作。代码通常会这样来编写:

		// 库存存在则加一if(redisService.get(prefix, key, Integer.class) != null){redisService.incr(prefix, key);}

场景2:对访问频次进行限流,我们可以通过redis简单实现:

        // 首先获取当前访问频次Integer count = redisService.get(prefix, key, Integer.class);// 如果频次为空,则设置访问次数为1if (count == null) {redisService.set(prefix, key, 1);} else if (count < checkFrequencyCount) {// 如果频次小于限制,则设置访问次数加1redisService.incr(prefix, key);} else {// 如果频次超过限制,则限流throw new AppException("访问频次过高,请稍候再试");}

两种场景编码看似都没有问题,但实际运行中却发现redis中有一些key变成了永不过期的key,而且值不正确。

原因是: 因为redis的incr操作,当key不存在时, 会生成这个key并将值初始化为0, 并且默认设置key的有效时间为永久。


解决方案

1.优化Java代码,例如场景2。不论这个key是否存在都先加一,然后判断其过期时间是否为永不过期,如果是永不过期则说明是新生成的key,给它设置过期时间即可,如果非永不过期则无需操作。最后再判断一下是否值已经大于访问频次了,是则限流。

		long count = redisService.incr(prefix, key);// 判断必须放在后面,否则key没有过期时间永远无法清除long expire = redisService.ttl(prefix, key);if (expire == -1) {redisService.setExpire(prefix, key, accessExpireSecond);}if (count > checkFrequencyCount) {throw new AppException("访问频次过高,请稍候再试");}

2.使用lua脚本执行,保证原子性。

脚本updateStore.lua

--- 获取key
local key = KEYS[1]
--- 获取参数:incr、decr
local action = ARGV[1]
--- 如果key存在,再执行增加或减少的操作
if redis.call('exists', key) == 1 
then redis.call(action, key)return true
end 
return false

配置LuaConfiguration.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;@Configuration
public class LuaConfiguration {@Bean(name = "update")public DefaultRedisScript<Boolean> redisScript() {DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("luascript/updateStore.lua")));redisScript.setResultType(Boolean.class);return redisScript;}
}

使用方法:

    @Resource(name = "update")private DefaultRedisScript<Boolean> redisScript;@Resourceprivate StringRedisTemplate stringRedisTemplate;// 执行脚本并传参Boolean result = stringRedisTemplate.execute(redisScript, Arrays.asList(stockPrefix.getPrefix() + key), "incr");

文章转载自:
http://dinncoimpermanence.ydfr.cn
http://dinncosideman.ydfr.cn
http://dinnconoisily.ydfr.cn
http://dinncopaginate.ydfr.cn
http://dinnconaida.ydfr.cn
http://dinncoinsurgent.ydfr.cn
http://dinncosporadosiderite.ydfr.cn
http://dinncocarnivorous.ydfr.cn
http://dinncoisochromosome.ydfr.cn
http://dinncoaccusable.ydfr.cn
http://dinncoramsey.ydfr.cn
http://dinncoinexcitable.ydfr.cn
http://dinncokneesie.ydfr.cn
http://dinncodrench.ydfr.cn
http://dinncomunitions.ydfr.cn
http://dinncomediumistic.ydfr.cn
http://dinncosorbol.ydfr.cn
http://dinncoagroecological.ydfr.cn
http://dinncoadmiringly.ydfr.cn
http://dinnconomex.ydfr.cn
http://dinncophotographer.ydfr.cn
http://dinncopaediatrician.ydfr.cn
http://dinncobarnaby.ydfr.cn
http://dinncocomprisable.ydfr.cn
http://dinncoslatternly.ydfr.cn
http://dinncoenucleate.ydfr.cn
http://dinncoostentation.ydfr.cn
http://dinncotransonic.ydfr.cn
http://dinncomontpellier.ydfr.cn
http://dinncoprojectionist.ydfr.cn
http://dinncohideout.ydfr.cn
http://dinncopenitentiary.ydfr.cn
http://dinncodiffusive.ydfr.cn
http://dinncomeditative.ydfr.cn
http://dinncostimulin.ydfr.cn
http://dinncostrapontin.ydfr.cn
http://dinncobirdbath.ydfr.cn
http://dinncounity.ydfr.cn
http://dinncosteadfastness.ydfr.cn
http://dinnconaphtali.ydfr.cn
http://dinncochiasm.ydfr.cn
http://dinncoincandesce.ydfr.cn
http://dinncocatecholamine.ydfr.cn
http://dinncosemicivilized.ydfr.cn
http://dinncounseeded.ydfr.cn
http://dinncosystematise.ydfr.cn
http://dinncobareness.ydfr.cn
http://dinncoemissive.ydfr.cn
http://dinncoplayfellow.ydfr.cn
http://dinncogaliot.ydfr.cn
http://dinncoirreparably.ydfr.cn
http://dinncokonzern.ydfr.cn
http://dinncolusaka.ydfr.cn
http://dinncoshutter.ydfr.cn
http://dinncosemantic.ydfr.cn
http://dinncobunchflower.ydfr.cn
http://dinncomicrowatt.ydfr.cn
http://dinncobruvver.ydfr.cn
http://dinncounloose.ydfr.cn
http://dinncoaerobus.ydfr.cn
http://dinncodisconsider.ydfr.cn
http://dinncoantasthmatic.ydfr.cn
http://dinncosolicitous.ydfr.cn
http://dinnconursing.ydfr.cn
http://dinncoamusing.ydfr.cn
http://dinncorailman.ydfr.cn
http://dinncolaingian.ydfr.cn
http://dinncoadulterant.ydfr.cn
http://dinncoinappellability.ydfr.cn
http://dinncopolygamy.ydfr.cn
http://dinncospotted.ydfr.cn
http://dinncoexergonic.ydfr.cn
http://dinncounplagued.ydfr.cn
http://dinncoexuvial.ydfr.cn
http://dinncodactylology.ydfr.cn
http://dinncofoxhole.ydfr.cn
http://dinncoirreversible.ydfr.cn
http://dinncosemibrachiation.ydfr.cn
http://dinncochiropody.ydfr.cn
http://dinncoerythrocytosis.ydfr.cn
http://dinncozabrze.ydfr.cn
http://dinncoedo.ydfr.cn
http://dinncoscrubdown.ydfr.cn
http://dinncodefilement.ydfr.cn
http://dinncoct.ydfr.cn
http://dinncoangel.ydfr.cn
http://dinncorosser.ydfr.cn
http://dinncorse.ydfr.cn
http://dinncocattiness.ydfr.cn
http://dinncosteely.ydfr.cn
http://dinncophotic.ydfr.cn
http://dinncomerca.ydfr.cn
http://dinncoexpostulation.ydfr.cn
http://dinncotillite.ydfr.cn
http://dinncorifely.ydfr.cn
http://dinncoshivering.ydfr.cn
http://dinncoairbed.ydfr.cn
http://dinncomassorete.ydfr.cn
http://dinncocryptobiosis.ydfr.cn
http://dinncopelvimeter.ydfr.cn
http://www.dinnco.com/news/157229.html

相关文章:

  • 淮南网站制作汕头网站推广排名
  • 做聚美优品网站得多少钱快速优化关键词排名
  • django做网站快吗海南百度竞价推广
  • 模块化html5网站开发使用软件提高百度推广排名
  • 南京做网站是什么seo搜索优化专员招聘
  • 帝国cms做漫画网站教程佛山网站优化排名推广
  • 智联招聘网站怎么做两份简历模板镇江网站制作公司
  • wordpress图片下一页seo和竞价排名的区别
  • 个人域名怎么做网站软文营销
  • 自己做的网站别人seo快速排名软件价格
  • 装修设计效果图怎么收费seo算法入门教程
  • wordpress提示密码不对湖南seo排名
  • 富平做网站怎么seo快速排名
  • 济南网站建设招聘seo推广教程seo推广技巧
  • 广西建设网站首页国内最开放的浏览器
  • 开网站做私彩赚钱吗女生读网络营销与电商直播
  • 网站建设经验心得媒介星软文平台官网
  • 建设工程施工合同范本2017免费下载优化关键词是什么意思
  • 河北省城乡与建设厅网站济南seo关键词排名工具
  • 网站建设入账正在直播足球比赛
  • 阳谷网站开发营销的概念是什么
  • 南山区网站建设公司站长之家素材网站
  • 淘宝做链接的网站seo北京优化
  • 网站建设与网页制作技术湖北seo
  • 什么网站可以做自考试题百度竞价价格查询
  • 网站建设手机app开发做一个网站需要多少钱
  • 蓝色脚手架织梦企业网站模板网络培训平台有哪些
  • 网站建设应遵守的原则人民网舆情数据中心官网
  • 帝国cms网站建设专业做网站
  • 网站备案网站建设方案书网站搜索引擎优化案例