Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ ignore:
- "**/ZRedisService.scala"
- "**/BenchmarkRuntime.scala"
- "**/CacheableBenchmarks.scala"
- "examples/**/*"
16 changes: 9 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ lazy val `smt` = (project in file("."))
`smt-csv`,
`smt-csv-derive`,
`smt-cache`,
`smt-common`
`smt-common`,
`scala2-11`,
`scala2-12`,
`scala2-13`
)
.settings(
commands ++= Commands.value,
Expand All @@ -208,13 +211,12 @@ lazy val `smt` = (project in file("."))
)
)

//FIXME root doesnot aggregate these examples
lazy val `scala2-13` = (project in file("examples/scala2-13"))
.settings(scalaVersion := scala213)
.settings(
libraryDependencies ++= Seq(
"org.bitlap" %% "smt-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-core" % lastVersionForExamples,
"org.bitlap" %% "smt-annotations" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-redis" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-caffeine" % lastVersionForExamples,
"dev.zio" %% "zio-redis" % zioRedisVersion,
Expand All @@ -230,8 +232,8 @@ lazy val `scala2-12` = (project in file("examples/scala2-12"))
.settings(scalaVersion := scala212)
.settings(
libraryDependencies ++= Seq(
"org.bitlap" %% "smt-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-core" % lastVersionForExamples,
"org.bitlap" %% "smt-annotations" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-redis" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-caffeine" % lastVersionForExamples,
"dev.zio" %% "zio-redis" % zioRedisVersion,
Expand All @@ -247,7 +249,7 @@ lazy val `scala2-11` = (project in file("examples/scala2-11"))
.settings(scalaVersion := scala211)
.settings(
libraryDependencies ++= Seq(
"org.bitlap" %% "smt-tools" % lastVersionForExamples
"org.bitlap" %% "smt-annotations" % lastVersionForExamples
)
)
.settings(
Expand Down
11 changes: 5 additions & 6 deletions examples/scala2-11/src/main/scala/org/bitlap/tools/Main.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.bitlap.tools

/**
*
* @author 梦境迷离
* @since 2021/6/16
* @version 1.0
/** @author
* 梦境迷离
* @since 2021/6/16
* @version 1.0
*/
object Main extends App {

@toString(includeInternalFields = true, includeFieldNames = true)
class TestClass(val i: Int = 0, var j: Int) {
val y: Int = 0
val y: Int = 0
var z: String = "hello"
var x: String = "world"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import zio.stream.ZStream

import scala.util.Random

/**
* use these function to test it.
/** use these function to test it.
*
* @author 梦境迷离
* @version 1.0,2022/3/18
* @author
* 梦境迷离
* @version 1.0,2022/3/18
*/
object CacheExample extends zio.App {

Expand All @@ -54,16 +54,15 @@ object CacheExample extends zio.App {
}

@cacheable // caffeine
def readStreamEntityFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] = {
def readStreamEntityFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] =
ZStream.fromEffect(ZIO.effect(CacheValue(Random.nextInt() + "")))
}

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
(for {
cache1 <- readStreamEntityFunction(1, "hello-world").runHead
cache2 <- updateStreamFunction(2, "helloworld").runHead
_ <- Utils.debug(s"${cache1.toString} ${cache2.toString}")
_ <- putStrLn("Hello good to meet you!")
_ <- Utils.debug(s"${cache1.toString} ${cache2.toString}")
_ <- putStrLn("Hello good to meet you!")
} yield ()).foldM(
e => Utils.debug(s"error => $e").exitCode,
_ => UIO.effectTotal(ExitCode.success)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.bitlap.tools
import zio.schema.{ DeriveSchema, Schema }

/**
*
* @author 梦境迷离
* @version 1.0,2022/3/22
/** @author
* 梦境迷离
* @version 1.0,2022/3/22
*/
case class CacheValue(i: String)

Expand Down
11 changes: 5 additions & 6 deletions examples/scala2-12/src/main/scala/org/bitlap/tools/Main.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.bitlap.tools

/**
*
* @author 梦境迷离
* @since 2021/6/16
* @version 1.0
/** @author
* 梦境迷离
* @since 2021/6/16
* @version 1.0
*/
object Main extends App {

@toString(includeInternalFields = true, includeFieldNames = true)
class TestClass(val i: Int = 0, var j: Int) {
val y: Int = 0
val y: Int = 0
var z: String = "hello"
var x: String = "world"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import zio.stream.ZStream
import zio.{ ExitCode, UIO, URIO, ZIO }
import org.bitlap.cacheable.core.cacheable

/**
* use these function to test it.
/** use these function to test it.
*
* @author 梦境迷离
* @version 1.0,2022/3/18
* @author
* 梦境迷离
* @version 1.0,2022/3/18
*/
object CacheExample extends zio.App {

Expand All @@ -54,16 +54,15 @@ object CacheExample extends zio.App {
}

@cacheable // caffeine
def readStreamEntityFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] = {
def readStreamEntityFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] =
ZStream.fromEffect(ZIO.effect(CacheValue(Random.nextInt() + "")))
}

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
(for {
cache1 <- readStreamEntityFunction(1, "hello-world").runHead
cache2 <- updateStreamFunction(2, "helloworld").runHead
_ <- Utils.debug(s"${cache1.toString} ${cache2.toString}")
_ <- putStrLn("Hello good to meet you!")
_ <- Utils.debug(s"${cache1.toString} ${cache2.toString}")
_ <- putStrLn("Hello good to meet you!")
} yield ()).foldM(
e => Utils.debug(s"error => $e").exitCode,
_ => UIO.effectTotal(ExitCode.success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package org.bitlap.tools

import zio.schema.{ DeriveSchema, Schema }

/**
*
* @author 梦境迷离
* @version 1.0,2022/3/22
/** @author
* 梦境迷离
* @version 1.0,2022/3/22
*/

// The case class should be here, not in the test
Expand Down
19 changes: 9 additions & 10 deletions examples/scala2-13/src/main/scala/org/bitlap/tools/Main.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.bitlap.tools

/**
*
* @author 梦境迷离
* @since 2021/6/16
* @version 1.0
/** @author
* 梦境迷离
* @since 2021/6/16
* @version 1.0
*/
object Main extends App {

@toString(includeInternalFields = true, includeFieldNames = true)
class TestClass(val i: Int = 0, var j: Int) {
val y: Int = 0
val y: Int = 0
var z: String = "hello"
var x: String = "world"
}
Expand All @@ -21,14 +20,14 @@ object Main extends App {
@toString(includeInternalFields = false, includeFieldNames = true)
@apply
@builder class A2(int: Int, val j: Int, var k: Option[String] = None, t: Option[Long] = Some(1L)) {
private val a: Int = 1
var b: Int = 1
private val a: Int = 1
var b: Int = 1
protected var c: Int = _

def helloWorld: String = "hello world"
}

println(A2(1, 2, None, None)) //use apply and toString
println(A2.builder().int(1).j(2).k(Option("hello")).t(None).build()) //use builder and toString
println(A2(1, 2, None, None)) // use apply and toString
println(A2.builder().int(1).j(2).k(Option("hello")).t(None).build()) // use builder and toString

}
4 changes: 4 additions & 0 deletions smt-cache/src/main/scala/org/bitlap/cache/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ object Cache {
)

override def clear(): Future[Unit] = cache.clear()

override def getAllT: Future[Map[K, cache.Out]] = cache.getAll
}

def getSyncCache[K, T <: Product](implicit
Expand Down Expand Up @@ -88,6 +90,8 @@ object Cache {
getT(key).flatMap(t => CaseClassExtractor.ofValue[cache.Out](t, field).asInstanceOf[Option[field.Field]])

override def clear(): Identity[Unit] = cache.clear()

override def getAllT: Identity[Map[K, cache.Out]] = cache.getAll
}

}
86 changes: 86 additions & 0 deletions smt-cache/src/main/scala/org/bitlap/cache/CacheAdapter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2022 bitlap
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.bitlap.cache

import scala.jdk.CollectionConverters._

import java.util.Collections

/** @author
* 梦境迷离
* @version 1.0,2022/7/5
*/
trait CacheAdapter[V] {
def getAllKeys: Set[String]

def putAll(map: Map[String, V]): Unit

def put(k: String, v: V): Unit

def get(k: String): V

def clear(): Unit
}

object CacheAdapter {

def adapted[V](cacheType: CacheStrategy): CacheAdapter[V] =
cacheType match {
case CacheStrategy.Lru(maxSize) =>
new LruHashMapCacheAdapter(
Collections.synchronizedMap(new java.util.LinkedHashMap[String, V](16, 0.75f, true) {
override def removeEldestEntry(eldest: java.util.Map.Entry[String, V]): Boolean = size > maxSize
})
)
case CacheStrategy.Normal =>
new ConcurrentMapCacheAdapter(new java.util.concurrent.ConcurrentHashMap[String, V]())
case CacheStrategy.CustomCacheStrategy(cacheAdapter) => cacheAdapter.asInstanceOf[CacheAdapter[V]]
}

class LruHashMapCacheAdapter[V](underlyingCache: java.util.Map[String, V]) extends CacheAdapter[V] {

override def getAllKeys: Set[String] = underlyingCache.keySet().asScala.toSet

override def putAll(map: Map[String, V]): Unit = underlyingCache.putAll(map.asJava)

override def put(k: String, v: V): Unit = underlyingCache.put(k, v)

override def get(k: String): V = underlyingCache.get(k)

override def clear(): Unit = underlyingCache.clear()
}

class ConcurrentMapCacheAdapter[V](underlyingCache: java.util.concurrent.ConcurrentMap[String, V])
extends CacheAdapter[V] {

override def getAllKeys: Set[String] = underlyingCache.keySet().asScala.toSet

override def putAll(map: Map[String, V]): Unit = underlyingCache.putAll(map.asJava)

override def put(k: String, v: V): Unit = underlyingCache.put(k, v)

override def get(k: String): V = underlyingCache.get(k)

override def clear(): Unit = underlyingCache.clear()
}

}
20 changes: 11 additions & 9 deletions smt-cache/src/main/scala/org/bitlap/cache/CacheKeyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,46 @@
package org.bitlap.cache

import java.util.UUID
import java.time.LocalDateTime
import java.time.ZonedDateTime

/** @author
* 梦境迷离
* @version 1.0,6/8/22
*/
trait CacheKeyBuilder[T] {
def generateKey(key: T): String

def unGenerateKey(key: String): T
}

object CacheKeyBuilder {

implicit val intKey: CacheKeyBuilder[Int] = new CacheKeyBuilder[Int] {
override def generateKey(key: Int): String = key.toString

override def unGenerateKey(key: String): Int = key.toInt
}

implicit val stringKey: CacheKeyBuilder[String] = new CacheKeyBuilder[String] {
override def generateKey(key: String): String = key

override def unGenerateKey(key: String): String = key
}

implicit val longKey: CacheKeyBuilder[Long] = new CacheKeyBuilder[Long] {
override def generateKey(key: Long): String = key.toString

override def unGenerateKey(key: String): Long = key.toLong
}

implicit val doubleKey: CacheKeyBuilder[Double] = new CacheKeyBuilder[Double] {
override def generateKey(key: Double): String = String.valueOf(key)

override def unGenerateKey(key: String): Double = key.toDouble
}

implicit val uuidKey: CacheKeyBuilder[UUID] = new CacheKeyBuilder[UUID] {
override def generateKey(key: UUID): String = key.toString
}

implicit val localDateTimeKey: CacheKeyBuilder[LocalDateTime] = new CacheKeyBuilder[LocalDateTime] {
override def generateKey(key: LocalDateTime): String = key.toString
}

implicit val zoneDateTimeKey: CacheKeyBuilder[ZonedDateTime] = new CacheKeyBuilder[ZonedDateTime] {
override def generateKey(key: ZonedDateTime): String = key.toString
override def unGenerateKey(key: String): UUID = UUID.fromString(key)
}
}
Loading