Skip to content

Instantly share code, notes, and snippets.

package utils.caching
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
import play.api.cache.CacheApi
object FutureCache {
def getOrElse[A](key: String, expiration: Duration = Duration.Inf)(orElse : => Future[A])(implicit cache: CacheApi, ct: ClassTag[A], executionContext: ExecutionContext): Future[A] = {
cache.get[A](key) match {
@mikesname
mikesname / RateLimitingController.scala
Created August 20, 2015 20:14
An extremely naive (doesn't properly handle concurrent accesses) rate limited action builder for POST requests.
trait RateLimitingController {
this: Controller with ControllerHelpers =>
// Abstract cache member
def cache: play.api.cache.CacheApi
// Function to check if current IP exceeds limit for request path
def checkRateLimit[A](implicit request: Request[A]): Boolean = {
val limit: Int = getConfigInt("ratelimit.limit")
val timeoutSecs: Int = getConfigInt("ratelimit.timeout")
@mikesname
mikesname / FutureCache.scala
Last active August 29, 2015 14:25
Caching a Future[T]
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
import play.api.cache.CacheApi
object FutureCache {
def getOrElse[A](key: String, expiration: Duration = Duration.Inf)(f: => Future[A])(implicit cache: CacheApi, ct: ClassTag[A], executionContext: ExecutionContext): Future[A] = {
cache.get[A](key) match {
case Some(a) => Future.successful(a)
case _ =>
@mikesname
mikesname / gist:7baece8eb46e7f8e4a3c
Created February 11, 2015 14:30
Play Slick question
import play.api.db.slick.DB
def allSamplesJson = Action { implicit request =>
request.session.get("l").map {
case "i" => DB.withSession { implicit dbs =>
val all = models.CodeSampleTable.allSamples()
Ok(Json.toJson(all))
}
case _ => Unauthorized(unautStr)
}.getOrElse {
name := """test-rjs"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala, SbtWeb)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
@mikesname
mikesname / gist:9027468
Last active August 29, 2015 13:56
Stackoverflow: How to replace all the values with the same key in a JSON tree
// Reply to: http://stackoverflow.com/questions/21801071/how-to-replace-all-the-values-with-the-same-key-in-a-json-tree/21804784#comment32996624_21804784
/**
* If the object matches an $oid, return the value and the new key
* Otherwise, return the original value
*/
def idOrValue(key: String, js: JsValue): (String, JsValue) = js \ "$oid" match {
case JsUndefined() => key -> js
case v => key -> v
}
@mikesname
mikesname / gist:8383440
Last active January 3, 2016 00:29
Basic Play optional authenticated action.
import play.api.mvc._
case class Context(email: Option[String])
class AuthenticatedRequest[A](val context: Context, request: Request[A]) extends WrappedRequest[A](request)
object MaybeAuthenticated extends ActionBuilder[AuthenticatedRequest] {
def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[SimpleResult]) = {
request.session.get("email").map { email =>
block(new AuthenticatedRequest(Context(Some(email)), request))
@mikesname
mikesname / GraphGist-SimpleRBAC.adoc
Last active November 13, 2024 21:03
Very simplistic way of doing role-based access control (RBAC) with Neo4j.

This is a very simple approach to doing role-based access control with Neo4j. It is optimistic, in the sense that all items are assumed to be world-readable unless they have specific constraints. Item visibility can be constrained to either individual users or all users who belong to a role. Roles are also hierarchical, so can inherit privileges from other roles.

First, lets create our basic example data:

@mikesname
mikesname / gist:5237809
Last active December 15, 2021 23:10
Example Play JSON Enum reading/writing
package enumtest
import play.api.libs.json._
object EnumUtils {
def enumReads[E <: Enumeration](enum: E): Reads[E#Value] = new Reads[E#Value] {
def reads(json: JsValue): JsResult[E#Value] = json match {
case JsString(s) => {
@mikesname
mikesname / gist:4594442
Last active December 11, 2015 11:29
Attempt to bodge together a Play 2.0 CLI management command... the file was bunged in the test package in order to have the running() and FakeApplication() utils available. It was executed with something like: play "run-main test.XMLtoGeoff"
package test
import play.api.test.Helpers.running
import play.api.test.FakeApplication
import models.Repository
import scala.io.Source
import controllers.Collections.processSource
import importers.USHMM