Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple-Spring-Memcached

Similar presentations


Presentation on theme: "Simple-Spring-Memcached"— Presentation transcript:

1 Simple-Spring-Memcached

2 Bio Nelz Carpentier Sr. Software Engineer at Widgetbox, Inc
10+ Years as a Software Engineer lz9999 n/nelz9999

3 Target Audience Technical Folks Familiar with Spring
May be interested in adding Memcached Understand the basics of Caching Understand the basics of Distributed Caching

4 Motivation Broke New Ground? Share My Learnings Self-Evident Concept
Not Too Smart Willing to do the work

5 Cross-Cutting Concern
Similar to Logging Similar to Authorization Main Code Flow doesn't need to know about it AOP to the Rescue!

6 Target Code / Infrastructure
Java Spring Serializable / Externalizable Objects High-Read, Low-Write Control / Wrap the Operations being AOP'd DB (or expensive data source) Backed Operational Ability to Support Memcached

7 SSM's Niche

8 Shoulders of Giants Build Maven Cobertura Test TestNG EasyMock Mirror
Runtime Spy Memcached Spring / AspectJ Log4J

9 Configure the ApplicationContext
<import resource="simplesm-context.xml" /> <bean id="memcachedConnectionBean" class= "net.nelz.simplesm.config.MemcachedConnectionBean"> <property name="consistentHashing" value="true" /> <property name="nodeList" value=" : :11311" /> </bean> <aop:aspectj-autoproxy />

10 Pseudocode of Read Through Scenario
$key = calculateValueCacheName() if cacheHasKey($key), return getFromCache($key) $value = doExpensiveRequestForValue() storeInCache($key, $value) return $value [PS: Pertinent Negative Null]

11 RTA - Example @ReadThroughAssignCache( assignedKey = "InTheWindow",
namespace = "DOG", expiration = 300) Dog getDogInWindow() { ... }

12 Generating Keys 255 Characters
Key + Namespace + Other Characters (~16) No Spaces Default: toString() Preferred: @CacheKeyMethod

13 Why Namespaces? Avoid Collisions Example: Auto-Increment Primary Keys
Vet:123 vs Pet:123

14 RTS – Example 1 @ReadThroughSingleCache( keyIndex = 0,
namespace = "VETS", expiration = 300) Vet getVet(Long id) { ... }

15 RTS – Example 2 @ReadThroughSingleCache( keyIndex = 1,
namespace = "VETS", expiration = 300) Vet getVet(Credentials cred, Long id) { ... }

16 Cardinality Options 1-to-1, Invariant “KEY” → Y @*AssignCache
1-to-1, 1-to-Many Key → Value Key → List<Value> @*SingleCache Many-to-Many List<Key> → List<Value> @*MultiCache

17 List → List Contract Requires: Agree in Length Agree in Sequence
Other Collections not (yet?) supported Subset Fall-Through

18 RTM - Example @ReadThroughMultiCache( keyIndex = 0,
namespace = "VETS", expiration = 300) List<Vet> getVet(List<Long> ids) { ... }

19 Pseudocode of Invalidate Scenario
$key = calculateValueCacheName() doUnderlyingOperations() deleteFromCache($key)

20 IAC – Example @InvalidateAssignCache( assignedKey = "InTheWindow",
namespace = "DOG") void removeFromWindow(Dog dog) { ... }

21 ISC – Example @InvalidateSingleCache( keyIndex = 0,
namespace = "FISH") void died(Fish fish) { ... }

22 IMC – Example @InvalidateMultiCache( keyIndex = 1, namespace = "DOG")
void sellLitter(Buyer to, List<Dog> dogs) { ... }

23 Pseudocode for Update Scenario
doUnderlyingOperations() $key = calculateValueCacheName() $value = harnessValueToStore() storeInCache($key, $value)

24 UAC - Example @UpdateAssignCache( assignedKey = "InTheWindow",
dataIndex = -1, namespace = "DOG", expiration = 300) Dog chooseDogForWindow() { ... }

25 USC - Example @UpdateSingleCache( keyIndex = 0, dataIndex = 0,
namespace = "FISH", expiration = 300) void addToInventory(Fish fish) { ... }

26 UMC - Example @UpdateMultiCache( keyIndex = 2, dataIndex = -1,
namespace = "DOG", expiration = 300) List<Dog> newLitter(Dog mom, Dog dad, List<Long> ids) { ... }

27 DEMO DEMO... Demo... Demo.

28 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

29 Best Practices Externalizable (!!!) Up to 8x faster!?!
Single Canonical Representation How to do aggregate collections Invalidates on Delete Updates on Save

30 What SSM Doesn't Do Stampede Protection or Soft Locks
Namespace Invalidation Multi-Invalidation / Method Multi-Parameter Keys

31 Possible Future Features
Multiple Logical Memcacheds Cache Observers Second-level cache

32 Version 2.0 Changes Package Structure
Remove Index Params, Adopt Annotations -1 0+ Key Calculation Spring Bean Nameable @ReturnValueKeyProvider( keyProviderBeanName = “myBeanName” )

33 Other Projects to Know About
Statsproxy - Monitoring Supported by Gear6 Brutis - Benchmarking Also by Gear6 Xmemcached Young Whalin Old, Stable Not Maintained?

34

35 Presentation and Demo Files
Blog Post: On Project Page: Presentation Demo Code Also on SlideShare Gliffy.com

36 Photo Credits http://www.flickr.com/photos/adulau/3086751588/

37 Questions Questions?


Download ppt "Simple-Spring-Memcached"

Similar presentations


Ads by Google