本文整理汇总了Scala中play.api.cache.Cache类的典型用法代码示例。如果您正苦于以下问题:Scala Cache类的具体用法?Scala Cache怎么用?Scala Cache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cache类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: WebSocketController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import akka.actor.{ActorSystem, _}
import akka.stream.Materializer
import play.api.libs.json._
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import models._
import play.api.cache.{Cache, CacheApi}
@Singleton
class WebSocketController @Inject()(implicit system: ActorSystem, materializer: Materializer, cache: CacheApi) {
object MyWebSocketActor {
def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}
class MyWebSocketActor(out: ActorRef) extends Actor {
override def preStart() {
println("open")
println(self.path.parent.toSerializationFormat)
val x = system.actorSelection(self.path.parent.toSerializationFormat)
x ! "Message12"
}
override def postStop() {
println("close")
}
override def receive = {
case request: JsValue =>
val response = handleMessage(request)
out ! response
}
def handleMessage(event: Event): JsValue = {
event match {
case event: EventA => {
val actorId = self.path.parent.toSerializationFormat
cache.set("actor-id_" + event.data, actorId)
Json.toJson(event)
}
case event: EventB => Json.toJson(event)
}
}
}
def socket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => MyWebSocketActor.props(out))
}
}
示例2: MultiLoginCacheIdContainer
//设置package包名称以及导入依赖的类
package jp.t2v.lab.play2.auth
import play.api.cache.Cache
import play.api.Play._
import scala.annotation.tailrec
import scala.util.Random
import java.security.SecureRandom
import de.frosner.broccoli.util.Logging
import scala.reflect.ClassTag
class MultiLoginCacheIdContainer[Id: ClassTag] extends IdContainer[Id] with Logging {
private[auth] val tokenSuffix = ":multitoken"
private[auth] val random = new Random(new SecureRandom())
def startNewSession(userId: Id, timeoutInSeconds: Int): AuthenticityToken = {
Logger.info(s"Starting new session for user '$userId'.")
val token = generate
store(token, userId, timeoutInSeconds)
token
}
@tailrec
private[auth] final def generate: AuthenticityToken = {
val table = "abcdefghijklmnopqrstuvwxyz1234567890_.~*'()"
val token = Iterator.continually(random.nextInt(table.size)).map(table).take(64).mkString
if (get(token).isDefined) generate else token
}
def remove(token: AuthenticityToken) {
Logger.info(s"Deleting session of user '${get(token)}'")
Cache.remove(token + tokenSuffix)
}
def get(token: AuthenticityToken): Option[Id] =
Cache.get(token + tokenSuffix).map(_.asInstanceOf[Id])
private[auth] def store(token: AuthenticityToken, userId: Id, timeoutInSeconds: Int) {
Cache.set(token + tokenSuffix, userId, timeoutInSeconds)
}
def prolongTimeout(token: AuthenticityToken, timeoutInSeconds: Int) {
get(token).foreach(store(token, _, timeoutInSeconds))
}
}
示例3: MultiLoginCacheIdContainerSpec
//设置package包名称以及导入依赖的类
package jp.t2v.lab.play2.auth
import play.api.cache.Cache
import play.api.test.{PlaySpecification, WithApplication}
class MultiLoginCacheIdContainerSpec extends PlaySpecification {
sequential
val container = new MultiLoginCacheIdContainer[String]
"starting a new session" should {
"work" in new WithApplication {
val user = "user"
val token = container.startNewSession(user, 1000)
Cache.get(token + container.tokenSuffix) === Some(user)
}
"time out" in new WithApplication {
val token = container.startNewSession("user", 1)
Thread.sleep(2000)
Cache.get(token + container.tokenSuffix) should beNone
}
}
"removing a session" should {
"work" in new WithApplication {
val user = "user"
val token = container.startNewSession(user, 1000)
container.remove(token)
Cache.get(token + container.tokenSuffix) should beNone
}
}
"getting a session" should {
"work" in new WithApplication {
val user = "user"
val token = container.startNewSession(user, 1000)
container.get(token) === Some(user)
}
}
"prolonging a session timeout" should {
"work" in new WithApplication {
val user = "user"
val token = container.startNewSession(user, 1)
container.prolongTimeout(token, 100)
Thread.sleep(2000)
Cache.get(token + container.tokenSuffix) === Some(user)
}
}
}
示例4: UserController
//设置package包名称以及导入依赖的类
package controllers
import play.api.Play.current
import play.api.cache.Cache
import play.api.libs.json._
import play.api.mvc.{Action, Controller, _}
object UserController extends Controller {
def authenticatedAction(f: Request[AnyContent] => Result) = Action {
request => {
(request.session.get("idToken").flatMap { idToken =>
Cache.getAs[JsValue](idToken + "profile")
} map {
profile => f(request)
}).orElse {
Some(Redirect(routes.IndexController.index()))
}.get
}
}
def index = authenticatedAction {
request => {
val idToken = request.session.get("idToken").get
val profile = Cache.getAs[JsValue](idToken + "profile").get
Ok(views.html.user(profile))
}
}
}