diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index e69de29..f08a188 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @kyri-petrou diff --git a/.github/workflows/auto-approve.yml b/.github/workflows/auto-approve.yml index ea318ad..4b9929d 100644 --- a/.github/workflows/auto-approve.yml +++ b/.github/workflows/auto-approve.yml @@ -5,9 +5,9 @@ on: jobs: auto-approve: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - - uses: hmarr/auto-approve-action@v2.2.0 + - uses: hmarr/auto-approve-action@v4 if: github.actor == 'scala-steward' || github.actor == 'renovate[bot]' with: github-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 87a8dd5..b03da68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,11 +1,9 @@ -# This file was autogenerated using `zio-sbt-ci` plugin via `sbt generateGithubWorkflow` +# This file was autogenerated using `zio-sbt-ci` plugin via `sbt ciGenerateGithubWorkflow` # task and should be included in the git repository. Please do not edit it manually. name: CI env: - JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags -Xmx6G -Xss4M -XX:+UseG1GC - JVM_OPTS: -XX:+PrintCommandLineFlags -Xmx6G -Xss4M -XX:+UseG1GC - NODE_OPTIONS: --max_old_space_size=6144 + JDK_JAVA_OPTIONS: -XX:+PrintCommandLineFlags 'on': workflow_dispatch: {} release: @@ -14,7 +12,12 @@ env: push: branches: - series/2.x - pull_request: {} + pull_request: + branches-ignore: + - gh-pages +concurrency: + group: ${{ github.workflow }}-${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.run_id || github.ref }} + cancel-in-progress: true jobs: build: name: Build @@ -22,38 +25,50 @@ jobs: continue-on-error: true steps: - name: Git Checkout - uses: actions/checkout@v3.3.0 + uses: actions/checkout@v4 with: fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev - name: Setup Scala - uses: actions/setup-java@v3.10.0 + uses: actions/setup-java@v4 with: - distribution: temurin + distribution: corretto java-version: '17' check-latest: true - - name: Check if the site workflow is up to date - run: free --si -tmws 10 & sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m checkGithubWorkflow + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Check all code compiles + run: free --si -tmws 10 & sbt +Test/compile - name: Check artifacts build process - run: sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m +publishLocal + run: sbt +publishLocal + - name: Check binary compatibility + run: sbt "+zioCacheJVM/mimaReportBinaryIssues; zioCacheJS/mimaReportBinaryIssues; zioCacheNative/mimaReportBinaryIssues" - name: Check website build process - run: free --si -tmws 10 & sbt docs/clean; sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m docs/buildWebsite + run: free --si -tmws 10 & sbt docs/clean; sbt docs/buildWebsite lint: name: Lint runs-on: ubuntu-latest continue-on-error: false steps: - name: Git Checkout - uses: actions/checkout@v3.3.0 + uses: actions/checkout@v4 with: fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev - name: Setup Scala - uses: actions/setup-java@v3.10.0 + uses: actions/setup-java@v4 with: - distribution: temurin + distribution: corretto java-version: '17' check-latest: true + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Check if the site workflow is up to date + run: free --si -tmws 10 & sbt ciCheckGithubWorkflow - name: Lint - run: free --si -tmws 10 & sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m lint + run: free --si -tmws 10 & sbt lint test: name: Test runs-on: ubuntu-latest @@ -62,31 +77,96 @@ jobs: fail-fast: false matrix: java: - - '8' - '11' - - '17' + - '21' scala-project: - - ++2.11.12 zioCacheJVM - - ++2.12.17 zioCacheJVM - - ++2.13.10 zioCacheJVM - - ++3.2.2 zioCacheJVM - - ++2.12.17 zioCacheJS - - ++2.13.10 zioCacheJS - - ++2.12.17 zioCacheNative - - ++2.13.10 zioCacheNative + - ++2.12 zioCacheJVM + - ++2.13 zioCacheJVM + - ++3.3 zioCacheJVM + - ++2.12 zioCacheJS + - ++2.13 zioCacheJS + - ++3.3 zioCacheJS + - ++2.12 zioCacheNative + - ++2.13 zioCacheNative + - ++3.3 zioCacheNative steps: + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev - name: Setup Scala - uses: actions/setup-java@v3.10.0 + uses: actions/setup-java@v4 with: - distribution: temurin + distribution: corretto java-version: ${{ matrix.java }} check-latest: true + - name: Cache Dependencies + uses: coursier/cache-action@v6 - name: Git Checkout - uses: actions/checkout@v3.3.0 + uses: actions/checkout@v4 with: fetch-depth: '0' - name: Test - run: free --si -tmws 10 & sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m ${{ matrix.scala-project }}/test + run: free --si -tmws 10 & sbt ${{ matrix.scala-project }}/test + update-readme: + name: Update README + runs-on: ubuntu-latest + continue-on-error: false + if: ${{ github.event_name == 'push' }} + steps: + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev + - name: Setup Scala + uses: actions/setup-java@v4 + with: + distribution: corretto + java-version: '17' + check-latest: true + - name: Cache Dependencies + uses: coursier/cache-action@v6 + - name: Generate Readme + run: free --si -tmws 10 & sbt docs/generateReadme + - name: Commit Changes + run: | + git config --local user.email "zio-assistant[bot]@users.noreply.github.com" + git config --local user.name "ZIO Assistant" + git add README.md + git commit -m "Update README.md" || echo "No changes to commit" + - name: Generate Token + id: generate-token + uses: zio/generate-github-app-token@v1.0.0 + with: + app_id: ${{ secrets.APP_ID }} + app_private_key: ${{ secrets.APP_PRIVATE_KEY }} + - name: Create Pull Request + id: cpr + uses: peter-evans/create-pull-request@v6 + with: + body: |- + Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. + + I will automatically update the README.md file whenever there is new change for README.md, e.g. + - After each release, I will update the version in the installation section. + - After any changes to the "docs/index.md" file, I will update the README.md file accordingly. + branch: zio-sbt-website/update-readme + commit-message: Update README.md + token: ${{ steps.generate-token.outputs.token }} + delete-branch: true + title: Update README.md + - name: Approve PR + if: ${{ steps.cpr.outputs.pull-request-number }} + run: gh pr review "$PR_URL" --approve + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_URL: ${{ steps.cpr.outputs.pull-request-url }} + - name: Enable Auto-Merge + if: ${{ steps.cpr.outputs.pull-request-number }} + run: gh pr merge --auto --squash "$PR_URL" || gh pr merge --squash "$PR_URL" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_URL: ${{ steps.cpr.outputs.pull-request-url }} ci: name: ci runs-on: ubuntu-latest @@ -103,30 +183,32 @@ jobs: runs-on: ubuntu-latest continue-on-error: false needs: - - build - - lint - - test + - ci if: ${{ github.event_name != 'pull_request' }} steps: - name: Git Checkout - uses: actions/checkout@v3.3.0 + uses: actions/checkout@v4 with: fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev - name: Setup Scala - uses: actions/setup-java@v3.10.0 + uses: actions/setup-java@v4 with: - distribution: temurin + distribution: corretto java-version: '17' check-latest: true + - name: Cache Dependencies + uses: coursier/cache-action@v6 - name: Release - run: free --si -tmws 10 & sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m ci-release + run: free --si -tmws 10 & sbt ci-release env: PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} PGP_SECRET: ${{ secrets.PGP_SECRET }} SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} - publish-docs: - name: Publish Docs + release-docs: + name: Release Docs runs-on: ubuntu-latest continue-on-error: false needs: @@ -134,61 +216,53 @@ jobs: if: ${{ ((github.event_name == 'release') && (github.event.action == 'published')) || (github.event_name == 'workflow_dispatch') }} steps: - name: Git Checkout - uses: actions/checkout@v3.3.0 + uses: actions/checkout@v4 with: fetch-depth: '0' + - name: Install libuv + run: sudo apt-get update && sudo apt-get install -y libuv1-dev - name: Setup Scala - uses: actions/setup-java@v3.10.0 + uses: actions/setup-java@v4 with: - distribution: temurin + distribution: corretto java-version: '17' check-latest: true + - name: Cache Dependencies + uses: coursier/cache-action@v6 - name: Setup NodeJs - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: 16.x registry-url: https://registry.npmjs.org - name: Publish Docs to NPM Registry - run: free --si -tmws 10 & sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m docs/publishToNpm + run: free --si -tmws 10 & sbt docs/publishToNpm env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - generate-readme: - name: Generate README + notify-docs-release: + name: Notify Docs Release runs-on: ubuntu-latest continue-on-error: false needs: - - release - if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event.action == 'published')) }} + - release-docs + if: ${{ (github.event_name == 'release') && (github.event.action == 'published') }} steps: - name: Git Checkout - uses: actions/checkout@v3.3.0 + uses: actions/checkout@v4 with: - ref: ${{ github.head_ref }} fetch-depth: '0' - - name: Setup Scala - uses: actions/setup-java@v3.10.0 - with: - distribution: temurin - java-version: '17' - check-latest: true - - name: Generate Readme - run: free --si -tmws 10 & sbt -J-XX:+UseG1GC -J-Xmx4g -J-Xms2g -J-Xss16m docs/generateReadme - - name: Commit Changes + - name: notify the main repo about the new release of docs package run: | - git config --local user.email "github-actions[bot]@users.noreply.github.com" - git config --local user.name "github-actions[bot]" - git add README.md - git commit -m "Update README.md" || echo "No changes to commit" - - name: Create Pull Request - uses: peter-evans/create-pull-request@v4.2.3 - with: - body: |- - Autogenerated changes after running the `sbt docs/generateReadme` command of the [zio-sbt-website](https://zio.dev/zio-sbt) plugin. - - I will automatically update the README.md file whenever there is new change for README.md, e.g. - - After each release, I will update the version in the installation section. - - After any changes to the "docs/index.md" file, I will update the README.md file accordingly. - branch: zio-sbt-website/update-readme - commit-message: Update README.md - delete-branch: true - title: Update README.md + PACKAGE_NAME=$(cat docs/package.json | grep '"name"' | awk -F'"' '{print $4}') + PACKAGE_VERSION=$(npm view $PACKAGE_NAME version) + curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${{ secrets.PAT_TOKEN }}"\ + https://api.github.com/repos/zio/zio/dispatches \ + -d '{ + "event_type":"update-docs", + "client_payload":{ + "package_name":"'"${PACKAGE_NAME}"'", + "package_version": "'"${PACKAGE_VERSION}"'" + } + }' diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml index 58bddce..be3535e 100644 --- a/.github/workflows/release-drafter.yml +++ b/.github/workflows/release-drafter.yml @@ -6,7 +6,7 @@ on: jobs: update_release_draft: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: release-drafter/release-drafter@v5 env: diff --git a/.nvmrc b/.nvmrc index e65243f..f3f52b4 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -16.19.0 +20.9.0 diff --git a/.sbtopts b/.sbtopts new file mode 100644 index 0000000..54ff2d4 --- /dev/null +++ b/.sbtopts @@ -0,0 +1,4 @@ +-J-Xms2G +-J-Xmx4G +-J-Xss16m +-J-XX:+UseG1GC diff --git a/README.md b/README.md index dce43c8..4ff7bf6 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ If multiple concurrent processes get the value at the same time the value will o In order to use this library, we need to add the following line in our `build.sbt` file: ```scala -libraryDependencies += "dev.zio" %% "zio-cache" % "0.2.2" +libraryDependencies += "dev.zio" %% "zio-cache" % "0.2.3" ``` ## Example @@ -128,11 +128,11 @@ Learn more on the [ZIO Cache homepage](https://zio.dev/zio-cache)! ## Contributing -For the general guidelines, see ZIO [contributor's guide](https://zio.dev/about/contributing). +For the general guidelines, see ZIO [contributor's guide](https://zio.dev/contributor-guidelines). ## Code of Conduct -See the [Code of Conduct](https://zio.dev/about/code-of-conduct) +See the [Code of Conduct](https://zio.dev/code-of-conduct) ## Support diff --git a/build.sbt b/build.sbt index 7b481b6..0b17121 100644 --- a/build.sbt +++ b/build.sbt @@ -1,13 +1,19 @@ -import Versions._ -import BuildHelper._ +import Versions.* +import BuildHelper.* +import zio.sbt.githubactions.Step enablePlugins(ZioSbtEcosystemPlugin, ZioSbtCiPlugin) +lazy val scala212V = "2.12.19" +lazy val scala213V = "2.13.14" +lazy val scala3V = "3.3.3" +lazy val allScalas = List("2.12", "2.13", "3.3") + inThisBuild( List( - name := "ZIO Cache", - crossScalaVersions -= scala3.value, - sbtBuildOptions := List("-J-XX:+UseG1GC", "-J-Xmx4g", "-J-Xms2g", "-J-Xss16m"), + name := "ZIO Cache", + zioVersion := "2.1.4", + scalaVersion := scala213V, ciBackgroundJobs := Seq("free --si -tmws 10"), developers := List( Developer( @@ -18,12 +24,13 @@ inThisBuild( ) ), ciEnabledBranches := Seq("series/2.x"), - supportedScalaVersions := - Map( - (zioCacheJVM / thisProject).value.id -> (zioCacheJVM / crossScalaVersions).value, - (zioCacheJS / thisProject).value.id -> (zioCacheJS / crossScalaVersions).value, - (zioCacheNative / thisProject).value.id -> (zioCacheNative / crossScalaVersions).value - ) + ciTargetJavaVersions := List("11", "21"), + ciTargetScalaVersions := Map( + (zioCacheJVM / thisProject).value.id -> allScalas, + (zioCacheJS / thisProject).value.id -> allScalas, + (zioCacheNative / thisProject).value.id -> allScalas + ), + versionScheme := Some("early-semver") ) ) @@ -32,7 +39,8 @@ addCommandAlias("benchmark", "benchmarks/Jmh/run") lazy val root = project .in(file(".")) .settings( - publish / skip := true, + publish / skip := true, + crossScalaVersions := Seq(), unusedCompileDependenciesFilter -= moduleFilter("org.scala-js", "scalajs-library") ) .aggregate( @@ -46,35 +54,53 @@ lazy val root = project lazy val zioCache = crossProject(JSPlatform, JVMPlatform, NativePlatform) .in(file("zio-cache")) .settings( - stdSettings(name = "zio-cache", packageName = Some("zio.cache"), enableCrossProject = true), + crossScalaVersions := List(scala212V, scala213V, scala3V), + stdSettings(name = Some("zio-cache"), packageName = Some("zio.cache"), enableCrossProject = true), silencerSettings, enableZIO(), libraryDependencies ++= Seq( "org.scala-lang.modules" %% "scala-collection-compat" % ScalaCollectionCompatVersion - ) + ), + scalacOptions ++= Seq( + "-release", + "11" + ), + scalacOptions ++= + (if (scalaBinaryVersion.value == "3") + Seq() + else + Seq( + "-opt:l:method", + "-opt:l:inline", + "-opt-inline-from:scala.**" + )) ) -lazy val zioCacheJS = zioCache.js - .settings(crossScalaVersions -= scala211.value, scalaJSUseMainModuleInitializer := true) - lazy val zioCacheJVM = zioCache.jvm .settings( - crossScalaVersions += scala3.value, scala3Settings, - scalaReflectTestSettings + scalaReflectTestSettings, + enableMimaSettingsJVM + ) + +lazy val zioCacheJS = zioCache.js + .settings( + scalaJSUseMainModuleInitializer := true, + enableMimaSettingsJS ) lazy val zioCacheNative = zioCache.native .settings( nativeSettings, - crossScalaVersions -= scala211.value + enableMimaSettingsNative ) lazy val benchmarks = project .in(file("zio-cache-benchmarks")) - .settings(stdSettings(name = "zio-cache-benchmarks", packageName = Some("zio.cache"))) + .settings(stdSettings(name = Some("zio-cache-benchmarks"), packageName = Some("zio.cache"))) .settings( - publish / skip := true + crossScalaVersions := List(scala213V, scala3V), + publish / skip := true ) .dependsOn(zioCacheJVM) .enablePlugins(JmhPlugin) @@ -85,12 +111,43 @@ lazy val docs = project moduleName := "zio-cache-docs", scalacOptions -= "-Yno-imports", scalacOptions -= "-Xfatal-warnings", - crossScalaVersions -= scala211.value, projectName := (ThisBuild / name).value, mainModuleName := (zioCacheJVM / moduleName).value, projectStage := ProjectStage.Development, ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(zioCacheJVM), + crossScalaVersions := List(scala213V), publish / skip := true ) .dependsOn(zioCacheJVM) .enablePlugins(WebsitePlugin) + +lazy val enforceMimaCompatibility = true // Enable / disable failing CI on binary incompatibilities + +lazy val enableMimaSettingsJVM = + Def.settings( + mimaFailOnProblem := enforceMimaCompatibility, + mimaPreviousArtifacts := previousStableVersion.value.map(organization.value %% moduleName.value % _).toSet, + mimaBinaryIssueFilters ++= Seq() + ) + +lazy val enableMimaSettingsJS = + Def.settings( + mimaFailOnProblem := enforceMimaCompatibility, + mimaPreviousArtifacts := previousStableVersion.value.map(organization.value %%% moduleName.value % _).toSet, + mimaBinaryIssueFilters ++= Seq() + ) + +lazy val enableMimaSettingsNative = + Def.settings( + mimaFailOnProblem := enforceMimaCompatibility, + mimaPreviousArtifacts := previousStableVersion.value.map(organization.value %%% moduleName.value % _).toSet, + mimaBinaryIssueFilters ++= Seq() + ) + +ThisBuild / ciCheckArtifactsBuildSteps += + Step.SingleStep( + "Check binary compatibility", + run = Some( + "sbt \"+zioCacheJVM/mimaReportBinaryIssues; zioCacheJS/mimaReportBinaryIssues; zioCacheNative/mimaReportBinaryIssues\"" + ) + ) diff --git a/project/BuildHelper.scala b/project/BuildHelper.scala index a2562e1..8c93dba 100644 --- a/project/BuildHelper.scala +++ b/project/BuildHelper.scala @@ -1,11 +1,5 @@ -import explicitdeps.ExplicitDepsPlugin.autoImport._ -import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._ -import sbt.Keys._ -import sbt.{Def, _} -import sbtbuildinfo.BuildInfoKeys._ -import sbtbuildinfo._ -import sbtcrossproject.CrossPlugin.autoImport._ -import scalafix.sbt.ScalafixPlugin.autoImport._ +import sbt.Keys.* +import sbt.{Def, *} object BuildHelper { val silencerSettings: Seq[Def.Setting[Task[Seq[String]]]] = @@ -13,6 +7,6 @@ object BuildHelper { if (Keys.scalaVersion.value.startsWith("3")) Seq.empty else - Seq("-P:silencer:globalFilters=[zio.stacktracer.TracingImplicits.disableAutoTrace]") + Seq("-Wconf:msg=[zio.stacktracer.TracingImplicits.disableAutoTrace]:silent") }) } diff --git a/project/Versions.scala b/project/Versions.scala index 06ce9d0..78405e3 100644 --- a/project/Versions.scala +++ b/project/Versions.scala @@ -1,5 +1,3 @@ object Versions { - val SilencerVersion = "1.7.12" - val IzumiReflectVersion = "2.2.5" - val ScalaCollectionCompatVersion = "2.9.0" + val ScalaCollectionCompatVersion = "2.12.0" } diff --git a/project/build.properties b/project/build.properties index 9a19778..be54e77 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version = 1.8.0 +sbt.version = 1.10.0 diff --git a/project/plugins.sbt b/project/plugins.sbt index 1802144..a2cf8b6 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,7 +1,10 @@ -val zioSbtVersion = "0.3.10+65-6a1d8191-SNAPSHOT" +val zioSbtVersion = "0.4.0-alpha.28" +val scalaNativeVersion = "0.4.17" -addSbtPlugin("dev.zio" % "zio-sbt-ecosystem" % zioSbtVersion) -addSbtPlugin("dev.zio" % "zio-sbt-website" % zioSbtVersion) -addSbtPlugin("dev.zio" % "zio-sbt-ci" % zioSbtVersion) +addSbtPlugin(("dev.zio" % "zio-sbt-ecosystem" % zioSbtVersion).exclude("org.scala-native", "sbt-scala-native")) +addSbtPlugin("dev.zio" % "zio-sbt-website" % zioSbtVersion) +addSbtPlugin("dev.zio" % "zio-sbt-ci" % zioSbtVersion) + +addSbtPlugin("org.scala-native" % "sbt-scala-native" % scalaNativeVersion) resolvers ++= Resolver.sonatypeOssRepos("public") diff --git a/zio-cache/js/src/main/scala/zio/cache/Platform.scala b/zio-cache/js/src/main/scala/zio/cache/Platform.scala index 3ad24d8..ae5c98e 100644 --- a/zio-cache/js/src/main/scala/zio/cache/Platform.scala +++ b/zio-cache/js/src/main/scala/zio/cache/Platform.scala @@ -16,11 +16,10 @@ package zio.cache -import com.github.ghik.silencer.silent - import java.util.{HashMap, Map} +import scala.annotation.nowarn -@silent("never used") +@nowarn("msg=never used") private object Platform { /** diff --git a/zio-cache/jvm/src/main/scala/zio/cache/Platform.scala b/zio-cache/jvm/src/main/scala/zio/cache/Platform.scala index ab16b08..20f3ea7 100644 --- a/zio-cache/jvm/src/main/scala/zio/cache/Platform.scala +++ b/zio-cache/jvm/src/main/scala/zio/cache/Platform.scala @@ -16,12 +16,11 @@ package zio.cache -import com.github.ghik.silencer.silent - import java.util.Map import java.util.concurrent.ConcurrentHashMap +import scala.annotation.nowarn -@silent("never used") +@nowarn("msg=never used") private object Platform { /** diff --git a/zio-cache/shared/src/main/scala/zio/cache/Cache.scala b/zio-cache/shared/src/main/scala/zio/cache/Cache.scala index 59a1a19..e7a4936 100644 --- a/zio-cache/shared/src/main/scala/zio/cache/Cache.scala +++ b/zio-cache/shared/src/main/scala/zio/cache/Cache.scala @@ -225,11 +225,11 @@ object Cache { promise.await case MapValue.Complete(key, exit, _, timeToLive) => trackAccess(key) - trackHit() if (hasExpired(timeToLive)) { map.remove(k, value) get(in) } else { + trackHit() ZIO.done(exit) } case MapValue.Refreshing( diff --git a/zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala b/zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala index 72efd39..984dac4 100644 --- a/zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala +++ b/zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala @@ -1,14 +1,17 @@ package zio.cache +import zio._ import zio.test.Assertion._ import zio.test._ -import zio._ object CacheSpec extends ZIOSpecDefault { def hash(x: Int): Int => UIO[Int] = y => ZIO.succeed((x ^ y).hashCode) + val identity: Int => UIO[Int] = + ZIO.succeed(_) + def spec: Spec[TestEnvironment with Scope, Any] = suite("CacheSpec")( test("cacheStats") { check(Gen.int) { salt => @@ -150,6 +153,18 @@ object CacheSpec extends ZIOSpecDefault { _ <- ZIO.forkAll(List.fill(1000)(task1)) _ <- cache.get(()).exit.timeoutFail("hanging")(3.seconds).forever.timeout(7.seconds) } yield assertCompletes - } @@ TestAspect.withLiveClock + } @@ TestAspect.withLiveClock, + test("cache hits should not be incremented for expired entries") { + for { + cache <- Cache.make(100, 1.second, Lookup(identity)) + _ <- cache.get(42) + _ <- TestClock.adjust(2.seconds) + _ <- cache.get(42) + cacheStats <- cache.cacheStats + hits = cacheStats.hits + misses = cacheStats.misses + } yield assertTrue(hits == 0L) && + assertTrue(misses == 2L) + } ) }