PsychoX
PsychoX

Reputation: 1098

Use Sedis pool is Play! Framework object

From Play! 2.4 (Scala) Sedis plugin should be injected in class using:

class TryIt @Inject()(sedisPool: Pool) extends Controller {
   val directValue: String = sedisPool.withJedisClient(client => client.get("someKey"))
}

But I need to use it in Scala object (I'm writing authorization filter, ani it need to be an object). Is it possible?

Upvotes: 0

Views: 255

Answers (1)

Aleksandar Stojadinovic
Aleksandar Stojadinovic

Reputation: 5049

As far as I see it, you are using the Sedis play plugin which supports the Guice injection model. However, if you use only Sedis, independent of the plugin, you can instantiate it as any other object, like this:

val pool = new Pool(new JedisPool(new JedisPoolConfig(), "localhost", 6379, 2000))

Edit: I just checked, it is completely ok to also inject the Pool into a filter. I tried it.

class ExampleFilter @Inject() (sedisPool: Pool) extends Filter {
override def apply(nextFilter: (RequestHeader) => Future[Result])(rh: RequestHeader): Future[Result] = 
 {
    println(this.sedisPool)
    //do your stuff.
    nextFilter(rh)
 }
}

This is possible because the filter sequence is also injected with Guice in 2.4.x.

Upvotes: 1

Related Questions