Simple Spring Memcached

download Simple Spring Memcached

If you can't read please download the document

Transcript of Simple Spring Memcached

Simple-Spring-Memcached

http://code.google.com/p/simple-spring-memcached/

A library that enables your code to benefit from distributed caching (via memcached), with minimal configuration and simple annotations.

Bio

Nelz Carpentier

Sr. Software Engineer at Widgetbox, Inc

10+ Years as a Software Engineer

http://www.nelz.net/

http://www.twitter.com/nelz9999

http://www.linkedin.com/in/nelz9999

Target Audience

Technical Folks

Familiar with Spring

May be interested in adding Memcached

Understand the basics of Caching

Understand the basics of Distributed Caching

Target Code / Infrastructure

Java

Spring

Serializable / Externalizable Objects

High-Read, Low-Write

You control (or can wrap) the methods you want to cache

Expensive Read Operations

Operational Ability to Support Memcached

SSM's Niche

Motivation

Broke New Ground?

Share My Learnings

Self-Evident Concept

Not Too SmartWilling to do the work

Shoulders of Giants

BuildMaven

Cobertura

TestTestNG

EasyMock

Mirror

RuntimeSpy Memcached

Spring / AspectJ

Log4J

Desired Characteristics of Caching

Needed in many layers

Should be transparent to main code flow

Similar characteristicsLogging

Security

Cross-cutting concern?

AOP to the Rescue!

Configure the ApplicationContext

Pseudocode of Read Through Scenario

$key = calculateValueCacheName()

if cacheHasKey($key), return getFromCache($key)

$value = doExpensiveRequestForValue()

storeInCache($key, $value)

return $value

Gets info out of the cache

Also back door of putting info into the cache

[PS: Pertinent Negative Null]

Generating Keys

255 Characters

Key + Namespace + Other Characters (~16)

No Spaces

Default: toString()

Preferred: @CacheKeyMethod

Why Namespaces?

Avoid Collisions

Example:Primary Keys

Bad: 123 vs 123

Good: Vet:123 vs Pet:123

RTA - Example

@ReadThroughAssignCache(

assignedKey = "InTheWindow",

namespace = "DOG",

expiration = 300)

Dog getDogInWindow() {

...

}

RTS Example 1

@ReadThroughSingleCache(

keyIndex = 0,

namespace = "VETS",

expiration = 300)

Vet getVet(Long id) {

...

}

RTS Example 2

@ReadThroughSingleCache(

keyIndex = 1,

namespace = "VETS",

expiration = 300)

Vet getVet(Credentials cred, Long id) {

...

}

Cardinality Options

1-to-1, 1-to-Many, InvariantKEY Y

KEY List

@ReadThroughAssignCache

1-to-1, 1-to-Many, ParameterizedKey Value

Key List

@ReadThroughSingleCache

Many-to-ManyList List

@ReadThroughMultiCache

List List Contract

Requires:Agree in Length

Agree in Sequence

Each Key/Value Pair is an individual cache entry

No Map support (yet?)

Subset Pass-Through

RTM - Example

@ReadThroughMultiCache(

keyIndex = 0,

namespace = "VETS",

expiration = 300)

List getVet(List ids) {

...

}

Pseudocode of Invalidate Scenario

$key = calculateValueCacheName()

doUnderlyingOperations()

deleteFromCache($key)

Removes info from the cache

IAC Example

@InvalidateAssignCache(

assignedKey = "InTheWindow",

namespace = "DOG")

void removeFromWindow(Dog dog) {

...

}

ISC Example

@InvalidateSingleCache(

keyIndex = 0,

namespace = "FISH")

void died(Fish fish) {

...

}

IMC Example

@InvalidateMultiCache(

keyIndex = 1,

namespace = "DOG")

void sellLitter(Buyer to, List dogs) {

...

}

Pseudocode for Update Scenario

doUnderlyingOperations()

$key = calculateValueCacheName()

$value = harnessValueToStore()

storeInCache($key, $value)

Puts info into the cache

UAC - Example

@UpdateAssignCache(

assignedKey = "InTheWindow",

dataIndex = -1,

namespace = "DOG",

expiration = 300)

Dog chooseDogForWindow() {

...

}

USC - Example

@UpdateSingleCache(

keyIndex = 0,

dataIndex = 0,

namespace = "FISH",

expiration = 300)

void addToInventory(Fish fish) {

...

}

UMC - Example

@UpdateMultiCache(

keyIndex = 2,

dataIndex = -1,

namespace = "DOG",

expiration = 300)

List newLitter(Dog mom, Dog dad, List ids) {

...

}

DEMO

DEMO...

Demo...

Demo...

Demo.

Protection from Exceptions

Exceptions from (your) underlying code should flow as normal

Exceptions within the caching code should be invisible to user, other than via log

Commons-logging / Log4J

Best Practices

Externalizable (!!!)Up to 8x faster!?!

Single Canonical RepresentationHow to do aggregate collections

Invalidates on Delete

Updates on Save

What SSM Doesn't Do

Stampede Protection or Soft Locks

Namespace Invalidation

Multi-Invalidation / Method

Multi-Parameter Keys

Possible Future Features

Multiple Logical Memcacheds

Cache Observers

Second-level cache

Version 2.0 Changes

Package Structure

Remove Index Params, Adopt Annotations-1 @ReturnValueKeyProvider

0+ @ParameterValueKeyProvider

Key CalculationSpring Bean Nameable

@ReturnValueKeyProvider(

keyProviderBeanName = myBeanName

)

Other Projects to Know About

Statsproxy - MonitoringSupported by Gear6

Brutis - BenchmarkingAlso by Gear6

XmemcachedYoung

WhalinOld, Stable

Not Maintained?

Presentation and Demo Files

Blog Post: http://nelz.net/2009/10/02/ssm-at-svcc/

On Project Page:http://code.google.com/p/simple-spring-memcached/

Presentation

Demo Code

Also on SlideShare

Gliffy.com

Photo Credits

http://www.flickr.com/photos/adulau/3086751588/

http://www.flickr.com/photos/edrabbit/3440426582/

http://www.flickr.com/photos/nielsvk/258851215/

http://www.flickr.com/photos/origomi/22442743/

http://www.flickr.com/photos/samcrockett/2213593924/

http://www.flickr.com/photos/themarkpike/83868756/

http://www.flickr.com/photos/bohman/210977249/

http://www.flickr.com/photos/red5standingby/235794106/

http://www.flickr.com/photos/roblee/375671072/

http://www.flickr.com/photos/andyandorla/239043213/

http://www.flickr.com/photos/chrisdag/1225937650/

http://www.flickr.com/photos/mechanikat/2727944748/

http://www.flickr.com/photos/expressmonorail/3694867130/

http://www.flickr.com/photos/ttstam/3833225072/

http://www.flickr.com/photos/vernhart/1574355240/

Questions

Questions?