zk中ReferenceCountedACLCache的作用是什么
这篇文章给大家介绍zk中ReferenceCountedACLCache的作用是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了靖远免费建站欢迎大家使用!
作用:完成LIst
空间复杂:内部类AtomicLongWithEquals
属性:
//日志信息 private static final Logger LOG = LoggerFactory.getLogger(ReferenceCountedACLCache.class); // long ACL 列表对应关系 final Map> longKeyMap = new HashMap >(); // ACL 列表 long对应关系 final Map , Long> aclKeyMap = new HashMap
, Long>(); final Map
referenceCounter = new HashMap (); private static final long OPEN_UNSAFE_ACL_ID = -1L; /** * these are the number of acls that we have in the datatree */ long aclIndex = 0;
方法:
记录引用次数
private static class AtomicLongWithEquals extends AtomicLong { private static final long serialVersionUID = 3355155896813725462L; public AtomicLongWithEquals(long i) { super(i); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } return equals((AtomicLongWithEquals) o); } public boolean equals(AtomicLongWithEquals that) { return get() == that.get(); } @Override public int hashCode() { return 31 * Long.valueOf(get()).hashCode(); } } 添加使用 public synchronized void addUsage(Long acl) { if (acl == OPEN_UNSAFE_ACL_ID) { return; } if (!longKeyMap.containsKey(acl)) { LOG.info("Ignoring acl " + acl + " as it does not exist in the cache"); return; } AtomicLong count = referenceCounter.get(acl); if (count == null) { referenceCounter.put(acl, new AtomicLongWithEquals(1)); } else { count.incrementAndGet(); } } //移除引用 public synchronized void removeUsage(Long acl) { if (acl == OPEN_UNSAFE_ACL_ID) { return; } if (!longKeyMap.containsKey(acl)) { LOG.info("Ignoring acl " + acl + " as it does not exist in the cache"); return; } long newCount = referenceCounter.get(acl).decrementAndGet(); if (newCount <= 0) { referenceCounter.remove(acl); aclKeyMap.remove(longKeyMap.get(acl)); longKeyMap.remove(acl); } } //如果引用计数值小于0,则移除相关信息 public synchronized void purgeUnused() { Iterator> refCountIter = referenceCounter.entrySet().iterator(); while (refCountIter.hasNext()) { Map.Entry entry = refCountIter.next(); if (entry.getValue().get() <= 0) { Long acl = entry.getKey(); aclKeyMap.remove(longKeyMap.get(acl)); longKeyMap.remove(acl); refCountIter.remove(); } } }
关于zk中ReferenceCountedACLCache的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页题目:zk中ReferenceCountedACLCache的作用是什么
当前路径:http://cdiso.cn/article/gjpepc.html