热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

id本地缓存策略

篇首语:本文由编程笔记#小编为大家整理,主要介绍了id本地缓存策略相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了id本地缓存策略相关的知识,希望对你有一定的参考价值。






思路是每次去生成id都要去获取比较慢,可以在启动的时候先获取部分id到缓存中,然后直接在缓存里获取id,当id数量下降到一定程度的时候再去补充id到缓存中


上配置类

package com.felix.spring_cloud_one.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(
prefix = "idsetting"
)
public class IdGenerateProperties
private IdGenerateProperties.IdentTooker identTooker = new IdGenerateProperties.IdentTooker();
public void setIdentTooker(IdentTooker identTooker)
this.identTooker = identTooker;

public IdentTooker getIdentTooker()
return identTooker;

public static class IdentTooker
private String enable = "false";
private Integer limitAllowExistNumber = 30;
private Integer maxKeepNumber = 200;
private Integer perRequestStep = 100;
public IdentTooker()

public String getEnable()
return this.enable;

public void setEnable(String enable)
this.enable = enable;

public Integer getLimitAllowExistNumber()
return this.limitAllowExistNumber;

public void setLimitAllowExistNumber(Integer limitAllowExistNumber)
this.limitAllowExistNumber = limitAllowExistNumber;

public Integer getMaxKeepNumber()
return this.maxKeepNumber;

public void setMaxKeepNumber(Integer maxKeepNumber)
this.maxKeepNumber = maxKeepNumber;

public Integer getPerRequestStep()
return this.perRequestStep;

public void setPerRequestStep(Integer perRequestStep)
this.perRequestStep = perRequestStep;




id获取及补充类

package com.felix.spring_cloud_one.util;
import com.felix.spring_cloud_one.config.ClientResult;
import com.felix.spring_cloud_one.config.IdGenerateProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
public class IdTook
private static final Logger log = LoggerFactory.getLogger(IdTook.class);
private static IdGenerateProperties idGenerateProperties;
private static IdentGenerateClient identGenerateClient;
private static final LinkedBlockingQueue<Long> IDENT_POOL &#61; new LinkedBlockingQueue<>();
//private static final LinkedBlockingQueue IDENT_POOL_LISTEN &#61; new LinkedBlockingQueue<>();
private static final ExecutorService SINGLE_THREAD_EXECUTOR;
private static IdTook.ListenRunnable listenRunnable;
static
SINGLE_THREAD_EXECUTOR &#61; new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), (r) ->
return new Thread(r, "id-took-thread");
, new ThreadPoolExecutor.DiscardOldestPolicy());
listenRunnable &#61; new IdTook.ListenRunnable();

public IdTook()

&#64;Autowired
public void setIdentFeignClient(IdentGenerateClient identGenerateClient)
//从id生成服务获取id
IdTook.identGenerateClient &#61; identGenerateClient;

&#64;Autowired
public void setDrawerProperties(IdGenerateProperties idGenerateProperties)
IdTook.idGenerateProperties &#61; idGenerateProperties;

&#64;PostConstruct
public void initIdentPool()
SINGLE_THREAD_EXECUTOR.execute(listenRunnable);

public static Long take()
log.info("当前取号池剩余:个号码", IDENT_POOL.size());
try
if (0 &#61;&#61; IDENT_POOL.size())
log.warn("取号线程异常中断&#xff0c;当前系统直接从生成器获取id");
ClientResult<Long> clientResult &#61; identGenerateClient.make();
if (1 &#61;&#61; clientResult.getCode())
return (Long)clientResult.getData();


Long id &#61; (Long)IDENT_POOL.poll(10L, TimeUnit.SECONDS);
if (IDENT_POOL.size() < idGenerateProperties.getIdentTooker().getLimitAllowExistNumber())
synchronized(listenRunnable)
listenRunnable.notify();


return id;
catch (InterruptedException var4)
log.error(var4.getMessage(), var4);
throw new RuntimeException("取号超时,请确保已启用id取号器功能");


private static void offer(Long... ids)
Long[] var1 &#61; ids;
int var2 &#61; ids.length;
for(int var3 &#61; 0; var3 < var2; &#43;&#43;var3)
Long id &#61; var1[var3];
try
IDENT_POOL.offer(id, 1L, TimeUnit.SECONDS);
catch (InterruptedException var6)
log.warn("插入新号码失败");



private static class ListenRunnable implements Runnable
private ListenRunnable()

public void run()
while(true)
synchronized(IdTook.listenRunnable)
if (IdTook.IDENT_POOL.size() > IdTook.idGenerateProperties.getIdentTooker().getLimitAllowExistNumber())
try
IdTook.listenRunnable.wait();
catch (InterruptedException var8)
var8.printStackTrace();

else
IdTook.log.info("取号池号码过少&#xff0c;获取新号码");
Integer step &#61; IdTook.idGenerateProperties.getIdentTooker().getPerRequestStep();
step &#61; step > 100 ? 100 : step;
int count &#61; (int)Math.ceil((double)(IdTook.idGenerateProperties.getIdentTooker().getMaxKeepNumber() / step));
count &#61; 0 &#61;&#61; count ? 1 : count;
for(int i &#61; 0; i < count; &#43;&#43;i)
ClientResult<Long []> clientResult &#61; IdTook.identGenerateClient.make(step);
if (1 &#61;&#61; clientResult.getCode())
Long[] data &#61; clientResult.getData();
IdTook.offer(data);








yml配置文件

idsetting:
identTooker:
limitAllowExistNumber: 5
maxKeepNumber: 5
perRequestStep: 5






推荐阅读
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 深入理解Tornado模板系统
    本文详细介绍了Tornado框架中模板系统的使用方法。Tornado自带的轻量级、高效且灵活的模板语言位于tornado.template模块,支持嵌入Python代码片段,帮助开发者快速构建动态网页。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
author-avatar
263企业邮箱温州授权合作伙伴
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有