Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
244 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package bigtop | ||
|
||
package object util { | ||
type Uuid = java.util.UUID | ||
} |
This file contains 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
76 changes: 76 additions & 0 deletions
76
redis/src/main/scala/bigtop/user/RedisSessionActions.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package bigtop | ||
package user | ||
|
||
import bigtop.json._ | ||
import bigtop.util._ | ||
import bigtop.problem._ | ||
import bigtop.concurrent._ | ||
import bigtop.concurrent.FutureImplicits._ | ||
import com.redis._ | ||
import com.redis.serialization.{ | ||
Format => RedisFormat, | ||
Parse => RedisParse | ||
} | ||
import com.weiglewilczek.slf4s.Logging | ||
import scalaz.syntax.validation._ | ||
import scalaz.std.option.optionSyntax._ | ||
|
||
trait RedisSessionActions[U <: User] extends SessionActions[U] { | ||
|
||
def userActions: UserActions[U] | ||
def sessionFormat: JsonFormat[Problem, Session[U]] | ||
|
||
def autoLogoutMillis: Option[Int] = None | ||
|
||
def keyPrefix: String = "session-" | ||
|
||
def redisPool: RedisClientPool = new RedisClientPool("localhost", 6379) | ||
|
||
def read(id: Uuid): FutureValidation[Problem, Session[U]] = { | ||
cache.get(id.toString).toSuccess(Problems.Client.noSession).fv | ||
} | ||
|
||
def save(session: Session[U]): FutureValidation[Problem, Session[U]] = { | ||
cache.set(session.id.toString, session) | ||
session.success.fv | ||
} | ||
|
||
def delete(id: Uuid): FutureValidation[Problem, Unit] = { | ||
cache.delete(id.toString) | ||
().success.fv | ||
} | ||
|
||
object cache extends SimpleRedisCache[Session[U]] { | ||
|
||
val pool = redisPool | ||
val prefix = keyPrefix | ||
val ttl = autoLogoutMillis | ||
|
||
def create(session: Session[U]): String = { | ||
val sessionId = Uuid.create.toString | ||
set(sessionId, session) | ||
sessionId | ||
} | ||
|
||
implicit object valueFormat extends RedisFormat({ | ||
case session: Session[U] => | ||
import blueeyes.json.Printer._ | ||
compact(render(sessionFormat.write(session))).getBytes("UTF-8") | ||
}) | ||
|
||
implicit object valueParse extends RedisParse[Session[U]]({ data: Array[Byte] => | ||
import blueeyes.json.JsonParser | ||
|
||
val session = | ||
for { | ||
json <- JsonParser.parseValidated(new String(data, "UTF-8")) | ||
session <- sessionFormat.read(json) | ||
} yield session | ||
|
||
session.fold( | ||
failure = f => sys.error(f.toString), | ||
success = s => s | ||
) | ||
}) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package bigtop.util | ||
|
||
import com.redis._ | ||
import com.redis.serialization.{ | ||
Format => RedisFormat, | ||
Parse => RedisParse | ||
} | ||
|
||
trait RedisCache[Key, Value] { | ||
def pool: RedisClientPool | ||
|
||
def encodeKey(key: Key): String | ||
|
||
def ttl: Option[Int] | ||
|
||
implicit def valueFormat: RedisFormat | ||
implicit def valueParse: RedisParse[Value] | ||
|
||
def set(key: Key, value: Value, ttl: Option[Int] = this.ttl): Unit = { | ||
pool.withClient { redis => | ||
ttl match { | ||
case Some(ttl) => redis.setex(encodeKey(key), ttl, value) | ||
case None => redis.set(encodeKey(key), value) | ||
} | ||
} | ||
} | ||
|
||
def get(key: Key, ttl: Option[Int] = this.ttl): Option[Value] = { | ||
pool.withClient { redis => | ||
val k = encodeKey(key) | ||
val v = redis.get[Value](k) | ||
ttl.foreach(redis.expire(k, _)) | ||
v | ||
} | ||
} | ||
|
||
def delete(key: Key): Unit = { | ||
pool.withClient { redis => | ||
redis.del(encodeKey(key)) | ||
} | ||
} | ||
} | ||
|
||
trait SimpleRedisCache[Value] extends RedisCache[String, Value] { | ||
def prefix: String | ||
|
||
final def encodeKey(key: String): String = { | ||
prefix + key | ||
} | ||
} |
This file contains 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
Oops, something went wrong.