未验证 提交 0b1b7010 编写于 作者: 梦境迷离's avatar 梦境迷离 提交者: GitHub

add examples (#161)

上级 207d4e8e
......@@ -23,11 +23,11 @@ jobs:
- name: Cache scala dependencies
uses: coursier/cache-action@v6
- name: Build
run: sbt +compile
- name: Checking headers
run: sbt headerCheckAll
- name: Build and Publish to Local
run: sbt +publishLocal
test:
runs-on: ubuntu-latest
......
......@@ -12,7 +12,7 @@ ThisBuild / resolvers ++= Seq(
lazy val scala212 = "2.12.14"
lazy val scala211 = "2.11.12"
lazy val scala213 = "2.13.8"
lazy val lastVersionForExamples = "0.3.4"
lazy val lastVersionForExamples = "0.4.0-SNAPSHOT"
lazy val commonSettings =
Seq(
......@@ -132,29 +132,44 @@ lazy val root = (project in file(".")).aggregate(tools, `cacheable-core`, `cache
.settings(
publishArtifact := false,
publish / skip := true,
headerLicense := Some(HeaderLicense.MIT("2022", "bitlap")) // otherwise headerCheckAll will failed
headerLicense := Some(HeaderLicense.MIT("2022", "bitlap"))
)
lazy val `scala2-13` = (project in file("examples/scala2-13")).settings(scalaVersion := scala213)
.settings(libraryDependencies ++= Seq(
"org.bitlap" %% "scala-macro-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-core" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-redis" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-caffeine" % lastVersionForExamples,
)).settings(
publish / skip := true,
excludeDependencies ++= Seq(
InclExclRule("com.google.protobuf")
),
Compile / scalacOptions += "-Ymacro-annotations"
)
lazy val `scala2-12` = (project in file("examples/scala2-12")).settings(scalaVersion := scala212)
.settings(libraryDependencies ++= Seq(
"org.bitlap" %% "scala-macro-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-core" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-redis" % lastVersionForExamples,
"org.bitlap" %% "smt-cacheable-caffeine" % lastVersionForExamples
)).settings(
publish / skip := true,
excludeDependencies ++= Seq(
InclExclRule("com.google.protobuf")
),
paradise()
)
lazy val `scala2-11` = (project in file("examples/scala2-11")).settings(scalaVersion := scala211)
.settings(libraryDependencies ++= Seq(
"org.bitlap" %% "scala-macro-tools" % lastVersionForExamples,
"org.bitlap" %% "smt-tools" % lastVersionForExamples,
)).settings(
excludeDependencies ++= Seq(
InclExclRule("com.google.protobuf")
),
publish / skip := true,
paradise()
)
......
......@@ -19,13 +19,13 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.bitlap.cacheable.redis
package org.bitlap.tools
import org.bitlap.cacheable.core.{ Cache, LogUtils }
import org.bitlap.cacheable.core.{ cacheable, Cache, LogUtils }
import org.bitlap.cacheable.redis.Implicits._
import zio.{ ExitCode, UIO, URIO, ZIO }
import zio.console.putStrLn
import zio.stream.ZStream
import zio.{ ExitCode, UIO, URIO, ZIO }
import scala.util.Random
......@@ -35,8 +35,9 @@ import scala.util.Random
* @author 梦境迷离
* @version 1.0,2022/3/18
*/
object UseCaseExample extends zio.App {
object CacheExample extends zio.App {
// import org.bitlap.cacheable.redis.Implicits._
def readAliasStreamFunction(id: Int, key: String): zio.stream.Stream[Throwable, String] = {
val $result = ZStream.fromEffect(ZIO.effect("hello world" + Random.nextInt()))
Cache($result)(List("UseCaseExample", "readAliasStreamFunction"), List(id, key))
......@@ -47,36 +48,21 @@ object UseCaseExample extends zio.App {
Cache($result)(List("UseCaseExample", "readStreamFunction"), List(id, key))
}
def readFunction(id: Int, key: String): ZIO[Any, Throwable, String] = {
val $result = ZIO.effect("hello world" + Random.nextInt())
Cache($result)(List("UseCaseExample", "readFunction"), List(id, key))
}
def updateStreamFunction(id: Int, key: String): ZStream[Any, Throwable, String] = {
val $result = ZStream.fromEffect(ZIO.effect("hello world" + Random.nextInt()))
Cache.evict($result)(List("readFunction1", "readFunction2"))
}
def updateFunction(id: Int, key: String): ZIO[Any, Throwable, String] = {
val $result = ZIO.effect("hello world" + Random.nextInt())
Cache.evict($result)(List("readFunction1", "readFunction2"))
}
def readEntityFunction(id: Int, key: String): ZIO[Any, Throwable, CacheValue] = {
val $result = ZIO.effect(CacheValue(Random.nextInt() + ""))
Cache($result)(List("UseCaseExample", "readEntityFunction"), List(id, key))
Cache.evict($result)(List("readFunction1", "readFunction2")) // not macro, not check whether read function is exists
}
@cacheable // caffeine
def readStreamEntityFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] = {
val $result = ZStream.fromEffect(ZIO.effect(CacheValue(Random.nextInt() + "")))
Cache($result)(List("UseCaseExample", "readStreamEntityFunction"), List(id, key))
ZStream.fromEffect(ZIO.effect(CacheValue(Random.nextInt() + "")))
}
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
(for {
ret <- readStreamEntityFunction(1, "hello-world").runHead
cache = zio.Runtime.default.unsafeRun(ZRedisService.hGet[CacheValue]("UseCaseExample-readStreamEntityFunction", "1-hello-world"))
_ <- LogUtils.debug(s"${ret.toString} $cache")
cache1 <- readStreamEntityFunction(1, "hello-world").runHead
cache2 <- updateStreamFunction(2, "helloworld").runHead
_ <- LogUtils.debug(s"${cache1.toString} ${cache2.toString}")
_ <- putStrLn("Hello good to meet you!")
} yield ()).foldM(
e => LogUtils.debug(s"error => $e").exitCode,
......
package org.bitlap.tools
import zio.schema.{ DeriveSchema, Schema }
/**
*
* @author 梦境迷离
* @version 1.0,2022/3/22
*/
case class CacheValue(i: String)
object CacheValue {
implicit val cacheValueSchema: Schema[CacheValue] = DeriveSchema.gen[CacheValue]
}
......@@ -19,15 +19,15 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.bitlap.cacheable.caffeine
package org.bitlap.tools
import org.bitlap.cacheable.caffeine.Implicits._
import scala.util.Random
import org.bitlap.cacheable.core.{ Cache, LogUtils }
import org.bitlap.cacheable.caffeine.Implicits._
import zio.console.putStrLn
import zio.stream.ZStream
import zio.{ ExitCode, UIO, URIO, ZIO }
import scala.util.Random
import org.bitlap.cacheable.core.cacheable
/**
* use these function to test it.
......@@ -35,8 +35,9 @@ import scala.util.Random
* @author 梦境迷离
* @version 1.0,2022/3/18
*/
object UseCaseExample extends zio.App {
object CacheExample extends zio.App {
// import org.bitlap.cacheable.caffeine.Implicits._
def readAliasStreamFunction(id: Int, key: String): zio.stream.Stream[Throwable, String] = {
val $result = ZStream.fromEffect(ZIO.effect("hello world" + Random.nextInt()))
Cache($result)(List("UseCaseExample", "readAliasStreamFunction"), List(id, key))
......@@ -47,36 +48,21 @@ object UseCaseExample extends zio.App {
Cache($result)(List("UseCaseExample", "readStreamFunction"), List(id, key))
}
def readFunction(id: Int, key: String): ZIO[Any, Throwable, String] = {
val $result = ZIO.effect("hello world" + Random.nextInt())
Cache($result)(List("UseCaseExample", "readFunction"), List(id, key))
}
def updateStreamFunction(id: Int, key: String): ZStream[Any, Throwable, String] = {
val $result = ZStream.fromEffect(ZIO.effect("hello world" + Random.nextInt()))
Cache.evict($result)(List("readFunction1", "readFunction2"))
}
def updateFunction(id: Int, key: String): ZIO[Any, Throwable, String] = {
val $result = ZIO.effect("hello world" + Random.nextInt())
Cache.evict($result)(List("readFunction1", "readFunction2"))
}
def readEntityFunction(id: Int, key: String): ZIO[Any, Throwable, CacheValue] = {
val $result = ZIO.effect(CacheValue(Random.nextInt() + ""))
Cache($result)(List("UseCaseExample", "readEntityFunction"), List(id, key))
Cache.evict($result)(List("readFunction1", "readFunction2")) // not macro, not check whether read function is exists
}
@cacheable // caffeine
def readStreamEntityFunction(id: Int, key: String): ZStream[Any, Throwable, CacheValue] = {
val $result = ZStream.fromEffect(ZIO.effect(CacheValue(Random.nextInt() + "")))
Cache($result)(List("UseCaseExample", "readStreamEntityFunction"), List(id, key))
ZStream.fromEffect(ZIO.effect(CacheValue(Random.nextInt() + "")))
}
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
(for {
ret <- readStreamEntityFunction(1, "hello-world").runHead
cache = zio.Runtime.default.unsafeRun(ZCaffeine.hGet[CacheValue]("UseCaseExample-readStreamEntityFunction", "1-hello-world"))
_ <- LogUtils.debug(s"${ret.toString} $cache")
cache1 <- readStreamEntityFunction(1, "hello-world").runHead
cache2 <- updateStreamFunction(2, "helloworld").runHead
_ <- LogUtils.debug(s"${cache1.toString} ${cache2.toString}")
_ <- putStrLn("Hello good to meet you!")
} yield ()).foldM(
e => LogUtils.debug(s"error => $e").exitCode,
......
package org.bitlap.tools
import zio.schema.{ DeriveSchema, Schema }
/**
*
* @author 梦境迷离
* @version 1.0,2022/3/22
*/
// The case class should be here, not in the test
case class CacheValue(i: String)
object CacheValue {
implicit val cacheValueSchema: Schema[CacheValue] = DeriveSchema.gen[CacheValue]
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册