12

How to cache the results in scala?

 2 years ago
source link: https://www.codesd.com/item/how-to-cache-the-results-in-scala.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to cache the results in scala?

advertisements

This page has a description of Map's getOrElseUpdate usage method:

object WithCache{
  val cacheFun1 = collection.mutable.Map[Int, Int]()
  def fun1(i:Int) = i*i
  def catchedFun1(i:Int) = cacheFun1.getOrElseUpdate(i, fun1(i))
}

So you can use catchedFun1 which will check if cacheFun1 contains key and return value associated with it. Otherwise, it will invoke fun1, then cache fun1's result in cacheFun1, then return fun1's result.

I can see one potential danger - cacheFun1 can became to large. So cacheFun1 must be cleaned somehow by garbage collector?

P.S. What about scala.collection.mutable.WeakHashMap and java.lang.ref.* ?


See the Memo pattern and the Scalaz implementation of said paper.

Also check out a STM implementation such as Akka.

Not that this is only local caching so you might want to lookinto a distributed cache or STM such as CCSTM, Terracotta or Hazelcast

Related Articles

How to cache the results of a Spring Data JPA query method without using the query cache?

I have a Spring Boot app with Spring Data JPA (hibernate backend) repository classes. I've added a couple custom finder methods, some with specific @Query annotation to tell it how to get the data. I have already set up EhCache for the hibernate 2nd

How to cache the results

I heard that a lot of big project administrators caches results. I wonder how do I do it? Thanks I'm using PHP.Pseudo-logic: Test if query result exists in cache If so, retrieve from cache return result else execute query against database transfer re

How to cache the results of a WCF service call

I have a WCF service which is called in 4 different places in my system. It returns approx 500 records from the database each time it's called. I would like to use a cache in place of making the call to WCF every time because data in the DB will rema

Haskell Caches the Results of a Function

I have a function that takes a parameter and produces a result. Unfortunately, it takes quite long for the function to produce the result. The function is being called quite often with the same input, that's why it would be convenient if I could cach

How to store the result of an action in an apache spark using scala

How to store the result generated from an action like: count in an output directory, in apache Spark Scala? val countval= data.map((_,"")).reduceByKey((_+_)).count The below command does not work as count is not stored as RDD: countval.saveAsTex

How to stop the results of a cached jquery php load

I have a new problem which MUST affect practically every person who uses AJAX. I have a HTML/JavaScript page which (using a jQuery call) loads a .php into my page - but it seems to cache the results and often just returns the cached information rathe

How do I cache the result of the API call?

I'm calling an API method from a server: def get_data #......... get_some_data_from_server end I want to cache the result of this call, obviously. So I created a field in a table and changed the get_data to look like this: def fetch_data key = get_ca

Which is better: Analyze the big data every time from the database or cache the result?

I have a problem regarding the system performance. In the DB table there will be a big XML data in every record. My concern is that if I should parse the XML data each time from the DB to get the attributes and information in the XML. The other choic

Retrieving and caching the result in Select2

My application uses select2 to show list of names which is retrieved through Ajax call. It uses select2 ajax functionalities. But the problem is that select2 fetches items whenever i type on the select2 input. I dont want to fetch every time user typ

How to get the result of the select statement grouped by a column to perform a join statement on it?

How to get the result of select statement grouped by a column to perform join statement on it ?You should enclose the select statement that contains the GROUP BY instead of one of the joined table, something like this: SELECT t1.Id, .... FROM Table1

How to display the results from the search field in the main page in asp.net

i have a search-box in masterpage, in asp.net project and i dont know two things: 1) how to transfer the results to a new page (because its in master page) 2) how to display the results? for the first question i tried creating a Session with the Data

Caching the result of all methods

I have a class which is essentially a collection of methods for some data transformations. In another words I have some data in my files and I use a few different code snippets to transform the textual data into something that I can easily query. Now

Dynamic sql, how to save the result query in a new table?

Below is my table structure and I want to convert it into another format (From row to column type : StudentID | Mark | Subject ------------------------- 10 |46 |Java -------------------------- 10 |65 |C# -------------------------- 10 |79 |JavaScript

How to interpret the result of the Weka J48 classification

I need help to interpret result in weka using the J48 I dont know how to explain the result, I am using the dataset Heart Disease Data Set from http://archive.ics.uci.edu/ml/datasets/Heart+Disease And the J48 tree Please help me, with some points imp

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK