scala-lru-map

Simple LRU Map for caching

License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.snowplowanalytics
ArtifactId

ArtifactId

scala-lru-map_2.11
Last Version

Last Version

0.3.1
Release Date

Release Date

Type

Type

jar
Description

Description

scala-lru-map
Simple LRU Map for caching
Project URL

Project URL

https://github.com/snowplow-incubator/scala-lru-map
Project Organization

Project Organization

com.snowplowanalytics
Source Code Management

Source Code Management

https://github.com/snowplow-incubator/scala-lru-map

Download scala-lru-map_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.snowplowanalytics/scala-lru-map_2.11/ -->
<dependency>
    <groupId>com.snowplowanalytics</groupId>
    <artifactId>scala-lru-map_2.11</artifactId>
    <version>0.3.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.snowplowanalytics/scala-lru-map_2.11/
implementation 'com.snowplowanalytics:scala-lru-map_2.11:0.3.1'
// https://jarcasting.com/artifacts/com.snowplowanalytics/scala-lru-map_2.11/
implementation ("com.snowplowanalytics:scala-lru-map_2.11:0.3.1")
'com.snowplowanalytics:scala-lru-map_2.11:jar:0.3.1'
<dependency org="com.snowplowanalytics" name="scala-lru-map_2.11" rev="0.3.1">
  <artifact name="scala-lru-map_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.snowplowanalytics', module='scala-lru-map_2.11', version='0.3.1')
)
libraryDependencies += "com.snowplowanalytics" % "scala-lru-map_2.11" % "0.3.1"
[com.snowplowanalytics/scala-lru-map_2.11 "0.3.1"]

Dependencies

compile (6)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.typelevel : cats-core_2.11 jar 1.6.1
org.typelevel : cats-effect_2.11 jar 1.3.1
com.github.cb372 : scalacache-guava_2.11 jar 0.27.0
com.github.cb372 : scalacache-cats-effect_2.11 jar 0.27.0
com.google.guava : guava jar 28.1-jre

test (2)

Group / Artifact Type Version
org.scalacheck : scalacheck_2.11 jar 1.14.0
org.specs2 : specs2-core_2.11 jar 4.0.3

Project Modules

There are no modules declared in this project.

Scala LruMap

Build Status Maven Central codecov Join the chat at https://gitter.im/snowplow-incubator/scala-lru-map

A pure least recently used hash map based on java.util.LinkedHashMap.

API

Scala LruMap has "tagless final"-friendly API, allowing end-users to abstract over effects they use:

  • cats.effect.IO, ZIO or similar lazy referentially-transparent implementation for most production use cases
  • cats.data.State for testing purposes
  • Id when you need an eager implementation

Generally, we highly recommend to use a referentially-transparent implementation, but in some environments like Apache Spark or Apache Beam it is not possible to use lazy implementation. In Spark and similar environments we recommend to use Id, but everything needs to be tested in real environment.

Scala LruMap provides two tagless final "capabilities":

  • CreateLruMap[F[_], K, V], where K and V are type of key and values respectively and F is a type of effect. This capability tells its users that effect is capable of intializing a LruMap instance
  • LruMap[F[_], K, V] is more like a classical interface and simply exposes API for put and get for an entity. It could be a wrapper for for remote cache or an object encapsulating a mutable reference

Example Usage

import cats.effect.IO

class Fibonacci(lru: LruMap[IO, BigInt, BigInt]) {
  def calc(n: BigInt): IO[BigInt] = {
    if (n <= 0)
      IO.pure(0)
    else if (n == 1)
      IO.pure(1)
    else
      lru.get(n).flatMap(r => r match {
        case Some(r) => IO.pure(r)
        case None => for {
          m1 <- calc(n-1)
          m2 <- calc(n-2)
          _  <- lru.put(n, m1+m2)
        } yield m1+m2
      })
  }
}

val result = (for {
  // When the size of the map exceeds 500 the least recently used element is
  // removed
  lru    <- CreateLruMap[IO, BigInt, BigInt].create(500)
  result <- (new Fibonacci(lru)).calc(100)
} yield result).unsafeRunSync()

// Prints 354224848179261915075
println(result)

All impure methods and constructors are wrapped in cats.effect.Sync. When you want to use the result in an impure environment you can use unsafeRunSync as shown above.

Note on Thread Safety

Calls to lruMap.get and LruMap.set are not inherently thread-safe, so concurrency concerns are left up to the choice of Sync.

Copyright and license

Copyright 2012-2020 Snowplow Analytics Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License.

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.

com.snowplowanalytics

Snowplow Analytics: Incubator

Versions

Version
0.3.1
0.3.1-M2
0.3.1-M1
0.3.0
0.3.0-M3
0.3.0-M2
0.3.0-M1
0.2.0
0.2.0-M1
0.1.0
0.1.0-M2
0.1.0-M1