Giter Site home page Giter Site logo

play-mongo-cache's Introduction

play-mongo-cache

A mongo-based cache for Play applications

Motivation

The default Play cache is nice, but in many real-world applications, you want a cache that lives outside the web server process, because your web app might actually be running on multiple nodes (for load-balancing and/or failover reasons). A common scenario is a Heroku app with multiple dynos.

So you'll want a cache that resides in its own service, and can be accessed by all your web server processes. Memcached could do the trick. We started with memcached (via memcachier) but found it unreliable -- we had fairly frequent timeouts from the memcachier nodes. So we moved to Mongo, which has been working fine for us.

Project overview

This repo is a basic scala Play project (what you get when you init a new Play project) with a custom Cache plugin that uses a capped collection in Mongo as a cache. To use this in your own Play project, you should:

  1. Copy the code in util and service.cache into your projcet
  2. Copy the lines from conf/play.plugins into your play.plugins file
  3. Add the section at the end of conf/application.conf to your application.conf file
  4. Add the dependency on se.radley.play-plugins-salat to your Build.scala file

In your code, you can then

import play.api.cache.Cache
import play.api.Play.current

And use the Cache object as you would any other way:

Cache.getOrElse("some-key")
Cache.set("key", "some value", expiration = 100)

Notes

  • The starter play project was generated with Play 2.1.3. Sorry that's a bit old.
  • Change the name of your database in the mongo connection string in application.conf
  • Change the name of the collection to use in CachePlugin.scala
  • This is a fairly basic solution that's probably good enough for many use cases โ€” but it was not designed or tested for applications that require extreme throughput or ultra low latency or anything like that.

play-mongo-cache's People

Contributors

chrismyang avatar nelprin avatar

Watchers

 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.