Simple Spring Memcached

10,630 views

Published on

This is the presentation I will be giving on Sunday at Silicon Valley Code Camp 2009.

Published in: Technology, News & Politics
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
10,630
On SlideShare
0
From Embeds
0
Number of Embeds
542
Actions
Shares
0
Downloads
87
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Simple Spring Memcached

  1. 1. 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.
  2. 2. Bio <ul><li>Nelz Carpentier
  3. 3. Sr. Software Engineer at Widgetbox, Inc
  4. 4. 10+ Years as a Software Engineer
  5. 5. http://www.nelz.net/
  6. 6. http://www.twitter.com/nelz9999
  7. 7. http://www.linkedin.com/in/nelz9999 </li></ul>
  8. 8. Target Audience <ul><li>Technical Folks
  9. 9. Familiar with Spring
  10. 10. May be interested in adding Memcached
  11. 11. Understand the basics of Caching
  12. 12. Understand the basics of Distributed Caching </li></ul>
  13. 13. Target Code / Infrastructure <ul><li>Java
  14. 14. Spring
  15. 15. Serializable / Externalizable Objects
  16. 16. High-Read, Low-Write
  17. 17. You control (or can wrap) the methods you want to cache
  18. 18. Expensive Read Operations
  19. 19. Operational Ability to Support Memcached </li></ul>
  20. 20. SSM's Niche
  21. 21. Motivation <ul><li>Broke New Ground?
  22. 22. Share My Learnings
  23. 23. Self-Evident Concept
  24. 24. Not Too Smart </li><ul><li>Willing to do the work </li></ul></ul>
  25. 25. Shoulders of Giants <ul><li>Build </li><ul><li>Maven
  26. 26. Cobertura </li></ul><li>Test </li><ul><li>TestNG
  27. 27. EasyMock
  28. 28. Mirror </li></ul><li>Runtime </li><ul><li>Spy Memcached
  29. 29. Spring / AspectJ
  30. 30. Log4J </li></ul></ul>
  31. 31. Desired Characteristics of Caching <ul><li>Needed in many layers
  32. 32. Should be transparent to main code flow
  33. 33. Similar characteristics </li><ul><li>Logging
  34. 34. Security </li></ul><li>Cross-cutting concern?
  35. 35. AOP to the Rescue! </li></ul>
  36. 36. Configure the ApplicationContext <ul><li><import resource=&quot;classpath:simplesm-context.xml&quot; />
  37. 37. <bean id=&quot;memcachedConnectionBean&quot; class=
  38. 38. &quot;net.nelz.simplesm.config.MemcachedConnectionBean&quot;>
  39. 39. <property name=&quot;consistentHashing&quot; value=&quot;true&quot; />
  40. 40. <property name=&quot;nodeList&quot; value=&quot;127.0.0.1:11211 127.0.0.1:11311&quot; />
  41. 41. </bean>
  42. 42. <aop:aspectj-autoproxy /> </li></ul>
  43. 43. Pseudocode of Read Through Scenario <ul><li>$key = calculateValueCacheName()
  44. 44. if cacheHasKey($key), return getFromCache($key)
  45. 45. $value = doExpensiveRequestForValue()
  46. 46. storeInCache($key, $value)
  47. 47. return $value
  48. 48. Gets info out of the cache
  49. 49. Also back door of putting info into the cache
  50. 50. [PS: Pertinent Negative Null] </li></ul>
  51. 51. Generating Keys <ul><li>255 Characters
  52. 52. Key + Namespace + Other Characters (~16)
  53. 53. No Spaces
  54. 54. Default: toString()
  55. 55. Preferred: @CacheKeyMethod </li></ul>
  56. 56. Why Namespaces? <ul><li>Avoid Collisions
  57. 57. Example: </li><ul><li>Primary Keys
  58. 58. Bad: 123 vs 123
  59. 59. Good: Vet:123 vs Pet:123 </li></ul></ul>
  60. 60. RTA - Example <ul><li>@ReadThroughAssignCache(
  61. 61. assignedKey = &quot;InTheWindow&quot;,
  62. 62. namespace = &quot;DOG&quot;,
  63. 63. expiration = 300)
  64. 64. Dog getDogInWindow() {
  65. 65. ...
  66. 66. } </li></ul>
  67. 67. RTS – Example 1 <ul><li>@ReadThroughSingleCache(
  68. 68. keyIndex = 0,
  69. 69. namespace = &quot;VETS&quot;,
  70. 70. expiration = 300)
  71. 71. Vet getVet(Long id) {
  72. 72. ...
  73. 73. } </li></ul>
  74. 74. RTS – Example 2 <ul><li>@ReadThroughSingleCache(
  75. 75. keyIndex = 1,
  76. 76. namespace = &quot;VETS&quot;,
  77. 77. expiration = 300)
  78. 78. Vet getVet(Credentials cred, Long id) {
  79. 79. ...
  80. 80. } </li></ul>
  81. 81. Cardinality Options <ul><li>1-to-1, 1-to-Many, Invariant </li><ul><li>“ KEY” -> Y
  82. 82. “ KEY” -> List<Value>
  83. 83. @ReadThroughAssignCache </li></ul><li>1-to-1, 1-to-Many, Parameterized </li><ul><li>Key -> Value
  84. 84. Key -> List<Value>
  85. 85. @ReadThroughSingleCache </li></ul><li>Many-to-Many </li><ul><li>List<Key> -> List<Value>
  86. 86. @ReadThroughMultiCache </li></ul></ul>
  87. 87. List -> List Contract <ul><li>Requires: </li><ul><li>Agree in Length
  88. 88. Agree in Sequence </li></ul><li>Each Key/Value Pair is an individual cache entry
  89. 89. No Map support (yet?)
  90. 90. Subset Pass-Through </li></ul>
  91. 91. RTM - Example <ul><li>@ReadThroughMultiCache(
  92. 92. keyIndex = 0,
  93. 93. namespace = &quot;VETS&quot;,
  94. 94. expiration = 300)
  95. 95. List<Vet> getVet(List<Long> ids) {
  96. 96. ...
  97. 97. } </li></ul>
  98. 98. Pseudocode of Invalidate Scenario <ul><li>$key = calculateValueCacheName()
  99. 99. doUnderlyingOperations()
  100. 100. deleteFromCache($key)
  101. 101. Removes info from the cache </li></ul>
  102. 102. IAC – Example <ul><li>@InvalidateAssignCache(
  103. 103. assignedKey = &quot;InTheWindow&quot;,
  104. 104. namespace = &quot;DOG&quot;)
  105. 105. void removeFromWindow(Dog dog) {
  106. 106. ...
  107. 107. } </li></ul>
  108. 108. ISC – Example <ul><li>@InvalidateSingleCache(
  109. 109. keyIndex = 0,
  110. 110. namespace = &quot;FISH&quot;)
  111. 111. void died(Fish fish) {
  112. 112. ...
  113. 113. } </li></ul>
  114. 114. IMC – Example <ul><li>@InvalidateMultiCache(
  115. 115. keyIndex = 1,
  116. 116. namespace = &quot;DOG&quot;)
  117. 117. void sellLitter(Buyer to, List<Dog> dogs) {
  118. 118. ...
  119. 119. } </li></ul>
  120. 120. Pseudocode for Update Scenario <ul><li>doUnderlyingOperations()
  121. 121. $key = calculateValueCacheName()
  122. 122. $value = harnessValueToStore()
  123. 123. storeInCache($key, $value)
  124. 124. Puts info into the cache </li></ul>
  125. 125. UAC - Example <ul><li>@UpdateAssignCache(
  126. 126. assignedKey = &quot;InTheWindow&quot;,
  127. 127. dataIndex = -1,
  128. 128. namespace = &quot;DOG&quot;,
  129. 129. expiration = 300)
  130. 130. Dog chooseDogForWindow() {
  131. 131. ...
  132. 132. } </li></ul>
  133. 133. USC - Example <ul><li>@UpdateSingleCache(
  134. 134. keyIndex = 0,
  135. 135. dataIndex = 0,
  136. 136. namespace = &quot;FISH&quot;,
  137. 137. expiration = 300)
  138. 138. void addToInventory(Fish fish) {
  139. 139. ...
  140. 140. } </li></ul>
  141. 141. UMC - Example <ul><li>@UpdateMultiCache(
  142. 142. keyIndex = 2,
  143. 143. dataIndex = -1,
  144. 144. namespace = &quot;DOG&quot;,
  145. 145. expiration = 300)
  146. 146. List<Dog> newLitter(Dog mom, Dog dad, List<Long> ids) {
  147. 147. ...
  148. 148. } </li></ul>
  149. 149. DEMO <ul><li>DEMO...
  150. 150. Demo...
  151. 151. Demo...
  152. 152. Demo. </li></ul>
  153. 153. Protection from Exceptions <ul><li>Exceptions from (your) underlying code should flow as normal
  154. 154. Exceptions within the caching code should be invisible to user, other than via log
  155. 155. Commons-logging / Log4J </li></ul>
  156. 156. Best Practices <ul><li>Externalizable (!!!) </li><ul><li>Up to 8x faster!?! </li></ul><li>Single Canonical Representation </li><ul><li>How to do aggregate collections </li></ul><li>Invalidates on Delete
  157. 157. Updates on Save </li></ul>
  158. 158. What SSM Doesn't Do <ul><li>Stampede Protection or Soft Locks
  159. 159. Namespace Invalidation
  160. 160. Multi-Invalidation / Method
  161. 161. Multi-Parameter Keys </li></ul>
  162. 162. Possible Future Features <ul><li>Multiple Logical Memcacheds
  163. 163. Cache Observers
  164. 164. Second-level cache </li></ul>
  165. 165. Version 2.0 Changes <ul><li>Package Structure
  166. 166. Remove Index Params, Adopt Annotations </li><ul><li>-1 -> @ReturnValueKeyProvider
  167. 167. 0+ -> @ParameterValueKeyProvider </li></ul><li>Key Calculation </li><ul><li>Spring Bean Nameable
  168. 168. @ReturnValueKeyProvider(
  169. 169. keyProviderBeanName = “myBeanName”
  170. 170. ) </li></ul></ul>
  171. 171. Other Projects to Know About <ul><li>Statsproxy - Monitoring </li><ul><li>Supported by Gear6 </li></ul><li>Brutis - Benchmarking </li><ul><li>Also by Gear6 </li></ul><li>Xmemcached </li><ul><li>Young </li></ul><li>Whalin </li><ul><li>Old, Stable
  172. 172. Not Maintained? </li></ul></ul>
  173. 174. Presentation and Demo Files <ul><li>Blog Post: http://nelz.net/2009/10/02/ssm-at-svcc/
  174. 175. On Project Page: </li><ul><li>http://code.google.com/p/simple-spring-memcached/ </li></ul><li>Presentation
  175. 176. Demo Code
  176. 177. Also on SlideShare
  177. 178. Gliffy.com </li></ul>
  178. 179. Photo Credits <ul><li>http://www.flickr.com/photos/adulau/3086751588/
  179. 180. http://www.flickr.com/photos/edrabbit/3440426582/
  180. 181. http://www.flickr.com/photos/nielsvk/258851215/
  181. 182. http://www.flickr.com/photos/origomi/22442743/
  182. 183. http://www.flickr.com/photos/samcrockett/2213593924/
  183. 184. http://www.flickr.com/photos/themarkpike/83868756/
  184. 185. http://www.flickr.com/photos/bohman/210977249/
  185. 186. http://www.flickr.com/photos/red5standingby/235794106/
  186. 187. http://www.flickr.com/photos/roblee/375671072/
  187. 188. http://www.flickr.com/photos/andyandorla/239043213/
  188. 189. http://www.flickr.com/photos/chrisdag/1225937650/
  189. 190. http://www.flickr.com/photos/mechanikat/2727944748/
  190. 191. http://www.flickr.com/photos/expressmonorail/3694867130/
  191. 192. http://www.flickr.com/photos/ttstam/3833225072/
  192. 193. http://www.flickr.com/photos/vernhart/1574355240/ </li></ul>
  193. 194. Questions Questions?

×