Giter Club home page Giter Club logo

scakka-zoo-cache's Introduction

scakka-zoo-cache Build Status

A library that caches ZooKeeper data.

It is written in Scala and the cache synchronization is done using Akka.

The library caches data using a ZooKeeper instance, by specifying watches in the znodes that should be cached.

The paths and the subtrees to cache are defined using the library's API.

Features

  • Built on top of the ZooKeeper API
  • Cache the whole ZooKeeper tree, or just parts of it
  • Data synchronization using Akka Actors
  • Access with Scala, Akka, or Java messaging API
  • Deployed in the Maven Central:
<dependency>
  <groupId>com.github.astonbitecode</groupId>
  <artifactId>scakka-zoo-cache</artifactId>
  <version>0.1.0</version>
</dependency>

Scala API Usage

Initialize the cache

Assuming that zk is a ZooKeeper class instance, a ScakkaZooCache can be created like following:

####1. Using simple initialization

import com.github.astonbitecode.zoocache.ScakkaZooCacheFactory

val zooCache = ScakkaZooCacheFactory.scala(zk)

####2. Defining an ActorSystem

import com.github.astonbitecode.zoocache.ScakkaZooCacheFactory

val actorSystem = ActorSystem("myActorSystem")
val zooCache = ScakkaZooCacheFactory.scala(zk, actorSystem)

####3. Creating from inside an Akka Actor, using the ActorContext

import com.github.astonbitecode.zoocache.ScakkaZooCacheFactory

// This is one Actor
class MyActor extends Actor {

  // Create a zoocache instance
  val zooCache = ScakkaZooCacheFactory.scala(zk, context)

  // Handle messages
  override def receive(): Receive = {
    case _ => ... // Use the zoo cache
  }
}

Add a Path to the cache

Simply call the addPathToCache:

zooCache.addPathToCache("/cache/this/path")

Use the cache

val children = zooCache.getChildren("/a/path")
val data = zooCache.getData("/a/path")

Akka API Usage

Create the Actor that handles the Akka API messages

import com.github.astonbitecode.zoocache.ScakkaZooCacheFactory

// Create your ActorSystem
val actorSystem = ActorSystem("myActorSystem")

// Create a ScakkaZooCache
val zooCache = ScakkaZooCacheFactory.scala(zk, actorSystem)

// Get the Akka Props from the factory
val props = ScakkaZooCacheFactory.props()

// Create the ActorRef
val zooCacheActorRef = actorSystem.actorOf(props)

// Contact the ActorRef
zooCacheActorRef ! GetChildren("/a/path")

Add a path to the cache

zooCacheActorRef ! AddPathToCache("/a/path")

Use the cache

zooCacheActorRef ! GetChildren("/a/path")
zooCacheActorRef ! GetData("/a/path")

Akka messaging

The available messages for the Akka API exist in the com.github.astonbitecode.zoocache.api.akka package.

Each one of the available Akka messages has its corresponding response. This message is sent by the Actor that handles the Akka API as a response to a request. (You can consult the Scaladocs for more details).

For example, when sending a GetChildren message, a response of GetChildrenResponse will be received:

val askFuture = zooCacheActorRef ? GetChildren("/a/path")
val children: GetChildrenResponse = Await.result(askFuture, 30 seconds)

Request - Response correlation

Akka users are not obliged to use the Ask pattern. All the Akka messages offered by the ScakkaZooCache API have an Optional parameter that can be used for Request-Response correlation:

import com.github.astonbitecode.zoocache.ScakkaZooCacheFactory
import com.github.astonbitecode.zoocache.api.akka._

// This is one Actor
class MyActor extends Actor {

  // Create a zoocache instance
  val zooCache = ScakkaZooCacheFactory.scala(zk, context)
  // Create the zoocache Actor to handle the Akka API messages
  val zooCacheActorRef = context.actorOf(ScakkaZooCacheFactory.props(zooCache))

  // Send a message to the zooCacheActor
  zooCacheActorRef ! GetChildren("/a/path", Some("123"))

  // Handle the Responses
  override def receive(): Receive = {
    case getChildrenResponse: GetChildrenResponse => {
      if (getChildrenResponse.correlation == Some("123")) {
        println("OK")
      } else {
        println("CORRELATION ERROR")
      }
    }
    case _ =>
  }
}

Failure cases

In case of failure of any of the Akka API messages, the sender will receive a CacheFailure.

This message contains the failure details along with any correlation object the respective request contained.

scakka-zoo-cache's People

Contributors

astonbitecode avatar

Watchers

Kristján Oddsson avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.