eduardofcbg / redis-scala

Scala library for interfacing with Redis data-structures

GitHub

These are a set of utilities for easy interfacing with Redis. For now it's still incomplete, but more features will be added as they are needed.

Install

Using sbt, add the following resolver:

resolvers += "eduardofcbg" at "http://dl.bintray.com/eduardofcbg/maven"

The dependency is the following:

libraryDependencies += "com.github.eduardofcbg" %% "redis-scala-utils" % "0.02"

Usage

This is just a short guide. More detailed examples can be found on the tests.

Namespaces

The library starts by introducing namespaces. For this we take advantage of partially applied functions. Bellow we define a new object where different counters are needed.

  object Person {

    val counter = RedisCounter ("person:" + _) builder

  }
  
  object Student {
    
    val students = Person counter "student"
    val foreignStudents = Person counter "foreignstudents"
  
  }

The counter value inside the Person object is just a function. Such function needs to be applied to a desired key in the namespace. If no partial function is provided, the identity function is assumed and the namespace will just be a key.

  val simplerCounter = RedisCounter() builder "students"

Counters

Following the previous example, a counter can be used very simply:

  object Student {
    
    private val counter = Person counter "student"
    
    def total = counter.get
    
    def incr = counter.++
    
    def dcr = counter.--
  
  }

Lists

We extend the Redis Sorted Set with added functionality. Take a look at the following example.

  import scala.pickling.Defaults._
  import scala.pickling.json._
  import scala.pickling.{FastTypeTag, Pickler, Unpickler}

  object Student {
    
    val students = RedisList[Student] ("allstudents") builder
    
    val class1 = students "class1"
    val class2 = students "class2"
        
    def toClass1(student: Student, id: String) = class1 + (student, id)
    
    def toClass2(student: Student, id: String) = class2 + (student, id)
    
    def get(page: Long) = class1.union(class2)(page)
  
  }
  
  case class Student(name: String)

Even though the example is incomplete, it is evident how straightforward such a data model can be implemented.

Many-to-Many relationships

Bellow you find a another silly example, yet it shows how transparent relationships like these can become on the scala side.

  import scala.pickling.Defaults._
  import scala.pickling.json._
  import scala.pickling.{FastTypeTag, Pickler, Unpickler}

  object Student {
    
    val students = RedisManyToMany[Student, Class] (_.of) builder ("allstudents")
    
    def save(student: Student, id: String) = students + (student, id)
    
    def get(id: String) = students get id
    
    def getClasses(id: String) = get(id) map (_.of)
    
    def getForClass(name: String) = students getFor Class(name)
  
  }
  
  case class Student(name: String, of: List[Class])
  case class Class(name: String)

More

More utilities like these will be added in the future. In the meanwhile fell free to submit a pull request.