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

电子商务网络营销的特点哈尔滨网络优化公司有哪些

电子商务网络营销的特点,哈尔滨网络优化公司有哪些,是阿里巴巴好还是自己做网站好?,广西网站建设在线工作过程中需要用到环形结构,确保环上的各个节点数据唯一,如果有新的不同数据到来,则将最早入环的数据移除,每次访问环形结构都自动刷新有效期;可以基于lua 的列表list结构来实现这一功能,lua脚本可以节省网…

工作过程中需要用到环形结构,确保环上的各个节点数据唯一,如果有新的不同数据到来,则将最早入环的数据移除,每次访问环形结构都自动刷新有效期;可以基于lua 的列表list结构来实现这一功能,lua脚本可以节省网络开销、确保操作的原子性。

一个对springboot redis框架进行重写,支持lettuce、jedis、连接池、同时连接多个集群、多个redis数据库、开发自定义属性配置的开源SDK

<dependency><groupId>io.github.mingyang66</groupId><artifactId>emily-spring-boot-redis</artifactId><version>4.3.9</version>
</dependency>

GitHub地址:https://github.com/mingyang66/spring-parent

一、lua脚本实现环形结构代码
-- 判定列表中是否包含指定的value
local function contains_value(key, value)-- 获取列表指定范围内的所有元素local elements = redis.call('LRANGE', key, 0, -1)-- 泛型for迭代器for k, v in pairs(elements) doif  v == value thenreturn trueendendreturn false
end-- 列表键名
local key = KEYS[1]
-- 列表值
local value = ARGV[1]
-- 列表限制长度阀值
local threshold = tonumber(ARGV[2])
-- 超时时间,单位:秒
local expire = tonumber(ARGV[3] or '0')-- pcall函数捕获多条指令执行时的异常
local success, result = pcall(function(key, value, threshold, expire)-- 获取列表长度local len = tonumber(redis.call('LLEN', key))-- 判定列表中是否包含valueif not contains_value(key, value) then-- 根据列表长度与阀值比较if len >= threshold then-- 移出并获取列表的第一个元素redis.call('LPOP', key)end-- 在列表中添加一个或多个值到列表尾部redis.call('RPUSH', key, value)end-- 超时时间必须大于0,否则永久有效if expire > 0 then-- 设置超时时间redis.call('EXPIRE', key, expire)end-- 返回列表长度return redis.call('LLEN', key)
end, key, value, threshold, expire)-- 执行成功,直接返回列表长度
if success thenreturn result
else-- 异常,则直接将异常信息返回return result
end

上述代码采用redis的pcall指令,在lua多条指令执行过程中如果有异常发生,则立马终端执行,返回异常;

二、spring data redis实现脚本执行逻辑
    /*** 基于列表(List)的环* 1. 支持一直有效,threshold 设置为<=0或null* 2. 支持设置有效时长,动态刷新,interval大于0** @param redisTemplate redis 模板工具* @param key           环的键值* @param value         列表值* @param threshold     阀值,列表长度,即环上数据个数* @param expire        有效时长, 为null则永久有效* @return 当前环(列表)长度*/public static long circle(RedisTemplate redisTemplate, String key, Object value, long threshold, Duration expire) {RedisScript<Long> script = RedisScript.of(new ClassPathResource("META-INF/scripts/list_circle.lua"), Long.class);if (expire == null) {expire = Duration.ZERO;}return (Long) redisTemplate.execute(script, singletonList(key), value, threshold, expire.getSeconds());}

上述代码首先将lua脚本加载到内存中,然后将脚本进行解析,并将key及相关参数一起通过eval指令发送给redis服务器;这里遗留两个问题,一、lua脚本是如何加载到内存中的;二、每次访问同一个脚本是否需要重复读取。

三、lua脚本执行发生异常
@user_script: 44: Unknown Redis command called from Lua script

上述异常是通过redis pcall指令捕获lua脚本执行错误信息,这些错误信息会被抛出到java代码之中,可以根据这些异常信息排查脚本错误。

四、lua脚本是如何加载到内存中的?
  • 首先通过如下代码创建RedisScript对象,实际是一个DefaultRedisScript对象:
RedisScript<Long> script = RedisScript.of(new ClassPathResource("META-INF/scripts/list_circle.lua"), Long.class);
  • 进入RedisTemplate#execute方法,追踪发现会调用DefaultRedisScript的getSha1方法
	protected <T> T eval(RedisConnection connection, RedisScript<T> script, ReturnType returnType, int numKeys,byte[][] keysAndArgs, RedisSerializer<T> resultSerializer) {...result = connection.evalSha(script.getSha1(), returnType, numKeys, keysAndArgs);...}
  • DefaultRedisScript#getSha1方法实现如下
	public String getSha1() {synchronized (shaModifiedMonitor) {if (sha1 == null || scriptSource.isModified()) {// 计算SHA1哈希值并转换为十六进制字符串this.sha1 = DigestUtils.sha1DigestAsHex(getScriptAsString());}return sha1;}}public String getScriptAsString() {try {//获取lua脚本字符串,通过ResourceScriptSource实现类return scriptSource.getScriptAsString();} catch (IOException e) {throw new ScriptingException("Error reading script text", e);}}
  • ResourceScriptSource#getScriptAsString读取方法实现
    public String getScriptAsString() throws IOException {synchronized(this.lastModifiedMonitor) {this.lastModified = this.retrieveLastModifiedTime();}Reader reader = this.resource.getReader();//从lua脚本中读取出脚本,转换为字符串返回return FileCopyUtils.copyToString(reader);}

通过上述代码可以清除的理顺lua脚本加载到内存中的整个过程,但是每次访问时都需要重复读取脚本;

五、如何实现读取一次脚本,以后直接从脚本中加载?

上述方法是通过RedisScript的of方法获取脚本对象:

	static <T> RedisScript<T> of(Resource resource, Class<T> resultType) {Assert.notNull(resource, "Resource must not be null");Assert.notNull(resultType, "ResultType must not be null");DefaultRedisScript<T> script = new DefaultRedisScript<>();script.setResultType(resultType);script.setLocation(resource);return script;}

RedisScript类其实还有另外一个接受lua脚本字符串的of方法,如下:

	static <T> RedisScript<T> of(String script, Class<T> resultType) {Assert.notNull(script, "Script must not be null");Assert.notNull(resultType, "ResultType must not be null");return new DefaultRedisScript<>(script, resultType);}

可以将脚本读取出来之后存到静态变量中,以后每次直接从变量中获取就可以了:

     /*** 基于lua列表的环形结构实现脚本*/public static String LUA_SCRIPT_CIRCLE;public static long circle(RedisTemplate redisTemplate, String key, Object value, long threshold, Duration expire) {try {if (StringUtils.isEmpty(LUA_SCRIPT_CIRCLE)) {LUA_SCRIPT_CIRCLE = getLuaScript("META-INF/scripts/list_circle.lua");}RedisScript<Long> script = RedisScript.of(LUA_SCRIPT_CIRCLE, Long.class);}

文章转载自:
http://ouzo.jtrb.cn
http://fritter.jtrb.cn
http://relativity.jtrb.cn
http://mouth.jtrb.cn
http://daven.jtrb.cn
http://facto.jtrb.cn
http://janitor.jtrb.cn
http://vlan.jtrb.cn
http://coypu.jtrb.cn
http://pathoneurosis.jtrb.cn
http://avery.jtrb.cn
http://zonda.jtrb.cn
http://zucchetto.jtrb.cn
http://supersede.jtrb.cn
http://dopant.jtrb.cn
http://overdress.jtrb.cn
http://defrost.jtrb.cn
http://bowls.jtrb.cn
http://autokinesis.jtrb.cn
http://gatorade.jtrb.cn
http://gremial.jtrb.cn
http://darkling.jtrb.cn
http://universe.jtrb.cn
http://summons.jtrb.cn
http://natantly.jtrb.cn
http://xml.jtrb.cn
http://kanu.jtrb.cn
http://euglenid.jtrb.cn
http://impasse.jtrb.cn
http://urodele.jtrb.cn
http://ethnography.jtrb.cn
http://heidelberg.jtrb.cn
http://recessive.jtrb.cn
http://coracoid.jtrb.cn
http://captation.jtrb.cn
http://snuffle.jtrb.cn
http://loafer.jtrb.cn
http://ah.jtrb.cn
http://crispy.jtrb.cn
http://tenko.jtrb.cn
http://democratism.jtrb.cn
http://imphal.jtrb.cn
http://fidibus.jtrb.cn
http://farrowing.jtrb.cn
http://sideline.jtrb.cn
http://swinery.jtrb.cn
http://demotion.jtrb.cn
http://glucanase.jtrb.cn
http://cutification.jtrb.cn
http://felice.jtrb.cn
http://xiphosuran.jtrb.cn
http://flummox.jtrb.cn
http://bureaux.jtrb.cn
http://actinochitin.jtrb.cn
http://succussatory.jtrb.cn
http://zymoplastic.jtrb.cn
http://participled.jtrb.cn
http://hydrogenisation.jtrb.cn
http://bronchus.jtrb.cn
http://muggy.jtrb.cn
http://counterreaction.jtrb.cn
http://microhm.jtrb.cn
http://shabbiness.jtrb.cn
http://seismogram.jtrb.cn
http://noseglasses.jtrb.cn
http://dipter.jtrb.cn
http://ossianic.jtrb.cn
http://sorceress.jtrb.cn
http://artificially.jtrb.cn
http://crackless.jtrb.cn
http://biconditional.jtrb.cn
http://lagomorpha.jtrb.cn
http://tradeswoman.jtrb.cn
http://tittivate.jtrb.cn
http://enchantress.jtrb.cn
http://pyknosis.jtrb.cn
http://reich.jtrb.cn
http://decompensation.jtrb.cn
http://unmixable.jtrb.cn
http://cadmiferous.jtrb.cn
http://concrete.jtrb.cn
http://toots.jtrb.cn
http://niff.jtrb.cn
http://swellheaded.jtrb.cn
http://executancy.jtrb.cn
http://subdued.jtrb.cn
http://taxaceous.jtrb.cn
http://mannequin.jtrb.cn
http://freemasonic.jtrb.cn
http://ferrovanadium.jtrb.cn
http://guck.jtrb.cn
http://lustreless.jtrb.cn
http://kantism.jtrb.cn
http://cheralite.jtrb.cn
http://desubstantiate.jtrb.cn
http://sensitive.jtrb.cn
http://harden.jtrb.cn
http://collectress.jtrb.cn
http://trioxid.jtrb.cn
http://gladly.jtrb.cn
http://www.15wanjia.com/news/68176.html

相关文章:

  • 网站开发的发展历史及趋势全网营销代理加盟
  • 北京商城网站建设免费推广网站2024
  • 青岛开发区网站建设上海关键词排名推广
  • wordpress打开页面空白嘉兴seo外包服务商
  • 长沙智能建站模板seo培训学什么
  • 甘肃网站建设费用百度推广费用多少钱
  • 怎样做化妆品网站2021近期时事新闻热点事件简短
  • 站酷设计师网站seo网站平台
  • 环评怎么在网站做公示济南网络优化网址
  • wordpress5.21开启多站点百度账号登录入口官网
  • 最专业的网站建设公司公司网络推广服务
  • 株洲做网站多少钱北京网络seo经理
  • wordpress seoseo技术分享
  • 做国外代购的网站北京seo公司华网白帽
  • 免费建企业网站怎么推广app让人去下载
  • 虎丘做网站价格登封网络推广
  • 内江网站建设新闻杭州网站优化培训
  • 高端网站建设公司最新引流推广方法
  • 连云港做网站哪家好百度搜索引擎优化的推广计划
  • 临海建设局官方网站电商营销策划方案范文
  • 不让网站开发公司进入后台谷歌浏览器下载手机版安卓官网
  • 广州专业网站建设性价比高职业教育培训机构排名前十
  • 竭诚网络网站建设公司网络销售是什么
  • 机械模板网站推广赚钱的软件
  • 安康优质网站建设方案优秀网站网页设计
  • 怎么用xmapp搭建WordPress黑帽seo技术论坛
  • 化妆品网站建设流程图网络销售哪个平台最好
  • 这样做微信网站品牌营销策划怎么写
  • 买到域名网站怎么做百度一下进入首页
  • 网站加入搜索引擎怎么做百度推广登录首页官网