May. 3rd, 2012

juan_gandhi: (Default)
A Boston-based financial services company has chosen Lift and Scala to create an amazing user experience for portfolio managers and they are looking to hire an excellent engineer to build the front end.

Think about the possibilities... working with a great team in Boston, using Lift and Scala every day, having clear responsibilities and metrics, and being able to build beautiful stuff that people will use every day.

If you are interested and have 5 years of web development experience and a CS degree, please email your resume to job501 [at] liftweb [dot] com. We will look at your resume and forward it to the hiring company. We also promise you a timely response.
juan_gandhi: (Default)
Короче, пока мы тут страдали по Трейвону, сенат штата Калифорния решил, что открытое ношение оружия незаконно (скрытое ещё при Рейгане запретили). Вот те здрасьте, как говорят, заняться нечем.

Тем временем по ящику, по pbs/kqed, посмотрел офигенный фильм с цветными киноархивами времён второй мировой. Вы посмотрите на варшавское гетто. Это не то, о чём обычно пишут в мемуарах. Как типа богатые евреи со своими бриллиантами... Это дети умирают от голода, лежат на улицах. Эти кадры сняты эсэсовцами, если что.

Меняя тему - прихожу домой чуть не в девять, нет предела радости от выполнения дневного задания (цитата из журнала "Корея"); за дверью в огород мяукает Офелия. Оголодала бедолага. Открыл - зашла. Оттаял треску (а что, вы думаете, держит архангельский мужик в морозилке, как не трешшочку? не гефилте же фишь) - пришла, пожрала, ушла. Дикая, ну.
juan_gandhi: (Default)
class CacheTest extends FlatSpec with ShouldMatchers {
  var myTime: Long = 0

  val sut = new Caching {
    def now = myTime
  }

  "A Cache" should "return the right stuff" in {
    var i: Int = 0
    val cache = sut.cacheFor(3, ()=>i)
    i = 17
    val n1: Int = cache()
    n1 should equal (17)
    i = 18
    val n2: Int = cache()
    n2 should equal (17)
    myTime = 2
    val n3: Int = cache()
    n3 should equal (17)
    myTime = 3
    val n4: Int = cache()
    n4 should equal (18)
  }
}
juan_gandhi: (Default)
What I'm looking for is a common generic solution for caching in Scala. Does not seem to be a big deal.

import java.util.concurrent.atomic.AtomicReference

trait Caching {

  protected def now: Long


  class Container[T](fun: () => T, expiring: Long) {
    lazy val value: T = fun()
    def isExpired = expiring <= now
  }

  class CacheUnit[T](fun:() => T, timeout: Long) {
    private val ref = new AtomicReference[Container[T]]
    def update = new Container(fun, now + timeout)
    ref set update

    def apply(): T = {
      val current = ref.get
      if (current.isExpired) ref.compareAndSet(current, update)
      ref.get.value
    }
  }

  def cacheFor[T] (timeout: Long, fun: () => T) = {

    new CacheUnit[T](fun, timeout)
  }

}

object Caching extends Caching {
  protected def now = System.currentTimeMillis
  
  implicit def currentValue[T](cu: CacheUnit[T]): T = cu.apply
}

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

August 2025

S M T W T F S
      12
3456789
10 11 12 13141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 16th, 2025 11:29 am
Powered by Dreamwidth Studios