All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getquill.context.cassandra.PrepareStatementCache.scala Maven / Gradle / Ivy

The newest version!
package io.getquill.context.cassandra

import com.datastax.oss.driver.shaded.guava.common.base.Charsets
import com.datastax.oss.driver.shaded.guava.common.cache.CacheBuilder
import com.datastax.oss.driver.shaded.guava.common.hash.Hashing

import java.util.concurrent.Callable

class PrepareStatementCache[V <: AnyRef](size: Long) {

  private val cache =
    CacheBuilder.newBuilder
      .maximumSize(size)
      .build[java.lang.Long, V]()

  private val hasher = Hashing.goodFastHash(128)

  def apply(stmt: String)(prepare: String => V): V =
    cache.get(
      hash(stmt),
      new Callable[V] {
        override def call: V = prepare(stmt)
      }
    )

  def invalidate(stmt: String): Unit = cache.invalidate(hash(stmt))

  private def hash(string: String): java.lang.Long =
    hasher
      .hashString(string, Charsets.UTF_8)
      .asLong()

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy