提交 94d4b70c 编写于 作者: 梦境迷离's avatar 梦境迷离

fix lock

上级 b5508ab2
......@@ -46,12 +46,15 @@ object Implicits {
override def getIfPresent(business: => ZStream[Any, Throwable, T])(identities: List[String], args: List[_]): ZStream[Any, Throwable, T] = {
val key = cacheKey(identities)
val field = cacheField(args)
for {
cacheValue <- ZStream.fromEffect(ZCaffeine.hGet[T](key, field))
_ <- if (ZCaffeine.disabledLog) ZStream.unit else LogUtils.debugS(s"Caffeine ZStream getIfPresent: identity:[$key],field:[$field],cacheValue:[$cacheValue]")
result <- cacheValue.fold(business.mapM(r => ZCaffeine.hSet(key, field, r).as(r)))(value => ZStream.fromEffect(ZIO.effectTotal(value)))
_ <- if (ZCaffeine.disabledLog) ZStream.unit else LogUtils.debugS(s"Caffeine ZStream getIfPresent: identity:[$key],field:[$field],result:[$result]")
} yield result
val locked = s"$key-$field"
locked.synchronized {
for {
cacheValue <- ZStream.fromEffect(ZCaffeine.hGet[T](key, field))
_ <- if (ZCaffeine.disabledLog) ZStream.unit else LogUtils.debugS(s"Caffeine ZStream getIfPresent: identity:[$key],field:[$field],cacheValue:[$cacheValue]")
result <- cacheValue.fold(business.mapM(r => ZCaffeine.hSet(key, field, r).as(r)))(value => ZStream.fromEffect(ZIO.effectTotal(value)))
_ <- if (ZCaffeine.disabledLog) ZStream.unit else LogUtils.debugS(s"Caffeine ZStream getIfPresent: identity:[$key],field:[$field],result:[$result]")
} yield result
}
}
}
......@@ -68,12 +71,15 @@ object Implicits {
override def getIfPresent(business: => ZIO[Any, Throwable, T])(identities: List[String], args: List[_]): ZIO[Any, Throwable, T] = {
val key = cacheKey(identities)
val field = cacheField(args)
for {
cacheValue <- ZCaffeine.hGet[T](key, field)
_ <- LogUtils.debug(s"Caffeine ZIO getIfPresent: identity:[$key], field:[$field], cacheValue:[$cacheValue]").unless(ZCaffeine.disabledLog)
result <- cacheValue.fold(business.tap(r => ZCaffeine.hSet(key, field, r).as(r)))(value => ZIO.effectTotal(value))
_ <- LogUtils.debug(s"Caffeine ZIO getIfPresent: identity:[$key], field:[$field], result:[$result]").unless(ZCaffeine.disabledLog)
} yield result
val locked = s"$key-$field"
locked.synchronized {
for {
cacheValue <- ZCaffeine.hGet[T](key, field)
_ <- LogUtils.debug(s"Caffeine ZIO getIfPresent: identity:[$key], field:[$field], cacheValue:[$cacheValue]").unless(ZCaffeine.disabledLog)
result <- cacheValue.fold(business.tap(r => ZCaffeine.hSet(key, field, r).as(r)))(value => ZIO.effectTotal(value))
_ <- LogUtils.debug(s"Caffeine ZIO getIfPresent: identity:[$key], field:[$field], result:[$result]").unless(ZCaffeine.disabledLog)
} yield result
}
}
}
}
......@@ -50,32 +50,16 @@ object ZCaffeine {
.expireAfterWrite(expireAfterWriteSeconds, TimeUnit.SECONDS).build[String, ConcurrentHashMap[String, Any]]
def hGet[T](key: String, field: String): Task[Option[T]] = {
key.synchronized {
ZIO.effect {
val hashMap = hashCache.getIfPresent(key)
if (hashMap == null || hashMap.isEmpty) {
ZIO.effect {
val hashMap = hashCache.getIfPresent(key)
if (hashMap == null || hashMap.isEmpty) {
None
} else {
val fieldValue = hashMap.get(field)
if (fieldValue == null || !fieldValue.isInstanceOf[T]) {
None
} else {
val fieldValue = hashMap.get(field)
if (fieldValue == null || !fieldValue.isInstanceOf[T]) {
None
} else {
Some(fieldValue.asInstanceOf[T])
}
}
}
}
}
def hDel(key: String, field: String): Task[Unit] = {
key.synchronized {
ZIO.effect {
val hashMap = hashCache.getIfPresent(key)
if (hashMap == null || hashMap.isEmpty) {
()
} else {
hashMap.remove(field)
hashCache.put(key, new ConcurrentHashMap(hashMap))
Some(fieldValue.asInstanceOf[T])
}
}
}
......@@ -90,17 +74,15 @@ object ZCaffeine {
}
def hSet(key: String, field: String, value: Any): Task[Unit] = {
key.synchronized {
ZIO.effect {
val hashMap = hashCache.getIfPresent(key)
if (hashMap == null || hashMap.isEmpty) {
val chm = new ConcurrentHashMap[String, Any]()
chm.put(field, value)
hashCache.put(key, chm)
} else {
hashMap.put(field, value)
hashCache.put(key, new ConcurrentHashMap(hashMap))
}
ZIO.effect {
val hashMap = hashCache.getIfPresent(key)
if (hashMap == null || hashMap.isEmpty) {
val chm = new ConcurrentHashMap[String, Any]()
chm.put(field, value)
hashCache.put(key, chm)
} else {
hashMap.put(field, value)
hashCache.put(key, new ConcurrentHashMap(hashMap))
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册