• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Cache类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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))

  }

} 
开发者ID:koduki,项目名称:example-palyframework,代码行数:58,代码来源:WebSocketController.scala


示例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))
  }

} 
开发者ID:FRosner,项目名称:cluster-broccoli,代码行数:50,代码来源:MultiLoginCacheIdContainer.scala


示例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)
    }

  }

} 
开发者ID:FRosner,项目名称:cluster-broccoli,代码行数:62,代码来源:MultiLoginCacheIdContainerSpec.scala


示例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))
    }
  }

} 
开发者ID:Krasnyanskiy,项目名称:auth0-scala-sample,代码行数:31,代码来源:UserController.scala



注:本文中的play.api.cache.Cache类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala PSQLException类代码示例发布时间:2022-05-23
下一篇:
Scala JSON类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap