Created
August 25, 2012 17:15
-
-
Save ericacm/3468084 to your computer and use it in GitHub Desktop.
Auto Updating Caching System - DateCacheActor.scala
This file contains hidden or 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
abstract class DateCacheActor[V](cacheSystem: CacheSystem) | |
extends CacheActor[V](cacheSystem) { | |
override def receive = findValueReceive orElse { | |
case UpdateCacheForNow => | |
updateCacheForNow() | |
case UpdateCacheForPreviousBusinessDay => | |
updateCacheForPreviousBusinessDay() | |
} | |
def updateCacheForNow() { | |
val activeBusinessDay: Range[Date] = DateUtil.calcActiveBusinessDay | |
val start = activeBusinessDay.getStart | |
val now = new Date | |
// If today is a business day and now is within the business day, | |
// retrieve data from the backend and put in the cache | |
if (now.getTime >= start.getTime && | |
now.getTime <= activeBusinessDay.getEnd.getTime) | |
updateCacheForDate(now) | |
} | |
} | |
def updateCacheForPreviousBusinessDay() { | |
updateCacheForDate(DateUtil.calcActiveBusinessDay.getStart) | |
} | |
def updateCacheForDate(date: Date) { | |
import DateCacheActor._ // Use separate thread pool | |
Future { findObject(new DateParams(date)) } | |
} | |
} | |
object DateCacheActor { | |
// Update cache for the current time | |
case object UpdateCacheForNow | |
// Update cache for previous business day | |
case object UpdateCacheForPreviousBusinessDay | |
// updateCacheForDate() uses a separate thread pool to prevent | |
// scheduled tasks from interfering with user requests | |
val FUTURE_POOL_SIZE = 5 | |
val FUTURE_QUEUE_SIZE = 20000 | |
private lazy val ucfdThreadPoolExecutor = | |
new ThreadPoolExecutor(FUTURE_POOL_SIZE, FUTURE_POOL_SIZE, | |
1, TimeUnit.MINUTES, | |
new ArrayBlockingQueue(FUTURE_QUEUE_SIZE, true)) | |
implicit lazy val ucfdExecutionContext: ExecutionContext = | |
ExecutionContext.fromExecutor(ucfdThreadPoolExecutor) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment