-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
163 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version = "3.7.14" | ||
version = "3.7.17" | ||
|
||
runner.dialect = scala213source3 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2020 Anton Sviridov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package subatomic.builders | ||
|
||
import subatomic.Logger | ||
|
||
import upickle.default._ | ||
|
||
trait Cache { | ||
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O): O | ||
|
||
def purge(): Unit | ||
|
||
def delete[I: ReadWriter](params: I): Unit | ||
} | ||
|
||
object Cache { | ||
object NoCaching extends Cache { | ||
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O) = | ||
compute | ||
|
||
def purge(): Unit = () | ||
def delete[I: ReadWriter](params: I): Unit = () | ||
} | ||
|
||
object InMemory extends Cache { | ||
private val mem = scala.collection.mutable.Map.empty[Any, Any] | ||
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O) = | ||
mem.getOrElseUpdate(params, compute).asInstanceOf[O] | ||
|
||
def purge(): Unit = mem.clear() | ||
|
||
def delete[I: ReadWriter](params: I): Unit = mem.remove(params) | ||
} | ||
|
||
class TrackingCache(cache: Cache) extends Cache { | ||
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O): O = { | ||
var hit = false | ||
val newCompute = () => { | ||
hit = true | ||
compute | ||
} | ||
val result = cache.produce(params)(newCompute()) | ||
|
||
if (hit) session += params | ||
|
||
result | ||
} | ||
|
||
def purge(): Unit = { | ||
cache.purge() | ||
clearSession() | ||
} | ||
|
||
def delete[I: ReadWriter](params: I): Unit = { | ||
cache.delete(params) | ||
session.filterInPlace(_ == params) | ||
} | ||
|
||
private val session = collection.mutable.ListBuffer.empty[Any] | ||
|
||
def clearSession(): Unit = session.clear() | ||
def allTracked: List[Any] = session.toList | ||
|
||
} | ||
|
||
def track(cache: Cache): TrackingCache = new TrackingCache(cache) | ||
|
||
def verbose(cache: Cache) = new Cache { | ||
override def produce[I: ReadWriter, O: ReadWriter]( | ||
params: I | ||
)(compute: => O): O = { | ||
var miss = false | ||
val newCompute = () => { | ||
miss = true | ||
compute | ||
} | ||
val result = cache.produce(params)(newCompute()) | ||
|
||
if (miss) Logger.default.logLine(s"Cache miss: ${params}") | ||
else Logger.default.logLine(s"Cache hit: ${params}") | ||
|
||
result | ||
} | ||
|
||
override def purge(): Unit = cache.purge() | ||
|
||
override def delete[I: ReadWriter](params: I): Unit = cache.delete(params) | ||
|
||
} | ||
|
||
def labelled(label: String, cache: Cache): Cache = new Cache { | ||
override def produce[I: ReadWriter, O: ReadWriter](params: I)( | ||
compute: => O | ||
): O = | ||
cache.produce((label, params))(compute) | ||
|
||
override def purge(): Unit = cache.purge() | ||
|
||
override def delete[I: ReadWriter](params: I): Unit = | ||
cache.delete((label, params)) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
modules/sbt-plugin/src/sbt-test/basic/dont-inherit-classpath/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
enablePlugins(SubatomicPlugin) | ||
|
||
libraryDependencies += "org.typelevel" %% "cats-effect" % "2.2.0" | ||
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.0" | ||
|
||
subatomicInheritClasspath := false | ||
|
||
scalaVersion := "3.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
modules/sbt-plugin/src/sbt-test/basic/inherit-classpath/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
enablePlugins(SubatomicPlugin) | ||
|
||
libraryDependencies += "org.typelevel" %% "cats-effect" % "2.2.0" | ||
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.0" | ||
|
||
scalaVersion := "3.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.8.3 | ||
sbt.version = 1.9.7 |