You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39Lines changed: 39 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1273,3 +1273,42 @@ Suggestions of what to cache:
1273
1273
* Fully rendered web pages
1274
1274
* Activity streams
1275
1275
* User graph data
1276
+
1277
+
### When to update the cache
1278
+
1279
+
Since you can only store a limited amount of data in cache, you'll need to determine which cache update strategy works best for your use case.
1280
+
1281
+
#### Cache-aside
1282
+
1283
+
<palign="center">
1284
+
<imgsrc="http://i.imgur.com/ONjORqk.png">
1285
+
<br/>
1286
+
<i><a href=http://www.slideshare.net/tmatyashovsky/from-cache-to-in-memory-data-grid-introduction-to-hazelcast>Source: From cache to in-memory data grid</a></i>
1287
+
</p>
1288
+
1289
+
The application is responsible for reading and writing from storage. The cache does not interact with storage directly. The application does the following:
1290
+
1291
+
* Look for entry in cache, resulting in a cache miss
1292
+
* Load entry from the database
1293
+
* Add entry to cache
1294
+
* Return entry
1295
+
1296
+
```
1297
+
def get_user(self, user_id):
1298
+
user = cache.get("user.{0}", user_id)
1299
+
if user is None:
1300
+
user = db.query("SELECT * FROM users WHERE user_id = {0}", user_id)
1301
+
if user is not None:
1302
+
cache.set(key, json.dumps(user))
1303
+
return user
1304
+
```
1305
+
1306
+
[Memcached](https://memcached.org/) is generally used in this manner.
1307
+
1308
+
Subsequent reads of data added to cache are fast. Cache-aside is also referred to as lazy loading. Only requested data is cached, which avoids filling up the cache with data that isn't requested.
1309
+
1310
+
##### Disadvantage(s): cache-aside
1311
+
1312
+
* Each cache miss results in three trips, which can cause a noticeable delay.
1313
+
* Data can become stale if it is updated in the database. This issue is mitigated by setting a time-to-live (TTL) which forces an update of the cache entry, or by using write-through.
1314
+
* When a node fails, it is replaced by a new, empty node, increasing latency.
0 commit comments