-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathREADME.Rmd
More file actions
537 lines (410 loc) · 12.5 KB
/
README.Rmd
File metadata and controls
537 lines (410 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE, error = TRUE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
eval = FALSE
)
system("docker stop mongobank redisbank postgresbank")
```
# bank
<!-- badges: start -->
[](https://github.com/ThinkR-open/bank/actions/workflows/R-CMD-check.yaml)
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
<!-- badges: end -->
The goal of `{bank}` is to provide alternative backends for caching with `{memoise}` & `{shiny}`.
## Installation
```{r}
# install.packages("remotes")
remotes::install_github("thinkr-open/bank")
```
## About
You're reading the doc about version : `r pkgload::pkg_version()`
This README has been compiled on the
```{r, eval = TRUE}
Sys.time()
```
Here are the test & coverage results :
```{r, eval = TRUE}
devtools::check(quiet = TRUE)
```
```{r echo = FALSE, eval = TRUE}
unloadNamespace("shinipsum")
```
```{r, eval = TRUE}
covr::package_coverage()
```
## Some things to know before starting
### Caching scope
When using `{bank}` backends with `{shiny}`, caching will done at the app-level, in other words the cache is stored across sessions.
Be aware of this behavior if you have sensitive data inside your app, as this might imply data leakage.
See `?shiny::bindCache`
> With an app-level cache scope, one user can benefit from the work done for another user's session. In most cases, this is the best way to get performance improvements from caching. However, in some cases, this could leak information between sessions. For example, if the cache key does not fully encompass the inputs used by the value, then data could leak between the sessions. Or if a user sees that a cached reactive returns its value very quickly, they may be able to infer that someone else has already used it with the same values.
### Cache flushing
As with any `{cachem}` compatible objects, the cache can be manually flushed using the `$reset()` method -- this will call `drop()` on MongoDb, `FLUSHALL` in Redis, & `DBI::dbRemoveTable()` + `DBI::dbCreateTable()` with Postgres.
```{r}
library(bank)
mongo_cache <- cache_mongo$new(
db = "bank",
url = "mongodb://localhost:27066",
prefix = "sn"
)
mongo_cache$reset()
```
As `{bank}` relies on external backends, it's probably better to let the DBMS handle the flushing of cache.
For example, in `redis.conf`, you can set :
```
maxmemory 2mb
maxmemory-policy allkeys-lru
```
LRU (least recently used) will allow redis to flush the key based on when they were used.
See <https://redis.io/topics/lru-cache>.
MongoDB doesn't come with a LRU mechanism, but you can set data to be ephemeral with [TTL index](https://docs.mongodb.com/manual/core/index-ttl/) inside your collection.
`{bank}` also tries to help with that by updating a `lastAccessed` date metadata field in Mongo whenever you `$get()` the key, meaning that you can implement your own caching strategy to evict least recently used cached objects.
### Postgre limitation
Postgre `bytea` column can only store up to 1GB elements, so you can't write a cache that's > 1GB.
## Backends
Note that if you want to use `{bank}` in a `{shiny}` app:
- `renderCachedPlot()` require `{shiny}` version 1.5.0 or higher
- `bindCache()` require `{shiny}` version 1.6.0 or higher
For now, the following backends are supported:
+ [MongoDB](#mongo)
+ [Redis](#redis)
+ [Postgre](#postgres)
### Mongo
Launching a container with mongo.
```{bash}
docker run --rm --name mongobank -d -p 27066:27017 -e MONGO_INITDB_ROOT_USERNAME=bebop -e MONGO_INITDB_ROOT_PASSWORD=aloula mongo:4
```
```{r echo = FALSE, eval = TRUE}
system("docker run --rm --name mongobank -d -p 27066:27017 -e MONGO_INITDB_ROOT_USERNAME=bebop -e MONGO_INITDB_ROOT_PASSWORD=aloula mongo:4")
Sys.sleep(20)
```
#### With `{memoise}`
First, the `cache_mongo` can be used
```{r eval = TRUE}
library(memoise)
library(bank)
# Create a mongo cache.
# The arguments will be passed to mongo::gridfs
mongo_cache <- cache_mongo$new(
db = "bank",
url = "mongodb://bebop:aloula@localhost:27066",
prefix = "sn"
)
f <- function(x) {
sample(1:1000, x)
}
mf <- memoise(f, cache = mongo_cache)
mf(5)
mf(5)
```
#### Inside `{shiny}`
Here is a first simple application that shows you the basics :
```{r}
library(shiny)
ui <- fluidPage(
# Creating a slider input that will be used as a cache key
sliderInput("nrow", "NROW", 1, 32, 32),
# Plotting a piece of mtcars
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderCachedPlot(
{
# Pretending this takes a long time
Sys.sleep(2)
plot(mtcars[1:input$nrow, ])
},
cacheKeyExpr = list(
# Defining the cache key
input$nrow
),
# Using our mongo cache
cache = mongo_cache
)
}
shinyApp(ui, server)
```
As you can see, the first time you set the slider to a given value, it takes a little bit to compute.
Then it's almost instantaneous.
Let's try a more complex application:
```{r}
# We'll put everything in a function so that it can later be reused with other backends
library(magrittr)
generate_app <- function(cache_object) {
ui <- fluidPage(
h1(
sprintf(
"Caching in an external DB using %s",
deparse(
substitute(cache_object)
)
)
),
sidebarLayout(
sidebarPanel = sidebarPanel(
# This sliderInput will be the cache key
# i.e we don't want to recompute the plot everytime
sliderInput("nrow", "Nrow", 1, 32, 32),
# Allow to clear the cache
actionButton("clear", "Clear Cache")
),
mainPanel = mainPanel(
# Outputing the reactive and a plot
verbatimTextOutput("txt"),
plotOutput("plot"),
# If you care about listing the cache keys
uiOutput("keys")
)
)
)
server <- function(input,
output,
session) {
# Our plot, cached using the cache object and
# watching the nrow
output$plot <- renderCachedPlot(
{
showNotification(
h2("I'm computing the plot"),
type = "message"
)
# Fake long computation
Sys.sleep(2)
# Plot
plot(mtcars[1:input$nrow, ])
},
# We cache on the input$nrow
cacheKeyExpr = list(
input$nrow
),
# The cache object is used here
cache = cache_object
)
rc <- reactive({
showNotification(
h2("I'm computing the reactive()"),
type = "message"
)
# Fake long computation
Sys.sleep(2)
input$nrow * 100
}) %>%
# Using bindCache() require shiny > 1.6.0
bindCache(
input$nrow,
cache = cache_object
)
output$txt <- renderText({
rc()
})
keys <- reactive({
# Listing the keys
invalidateLater(500)
cache_object$keys()
})
output$keys <- renderUI({
tags$ul(
lapply(keys(), tags$li)
)
})
observeEvent(input$clear, {
# Sometime you might want to remove everything from the cache
cache_object$reset()
showNotification(
h2("Cache reset"),
type = "message"
)
})
}
shinyApp(ui, server)
}
generate_app(mongo_cache)
```
#### Flushing MongoDB cache using LRU
All keys registered to MongoDB comes with a `metadata.lastAccessed` parameter.
Using this parameter, you'll be able to flush old cache if needed.
```{r eval = TRUE}
mongo <- mongolite::gridfs(
db = "bank",
url = "mongodb://bebop:aloula@localhost:27066",
prefix = "sn"
)
get_metadata <- function(mongo) {
purrr::map(mongo$find()$metadata, jsonlite::fromJSON)
}
Sys.sleep(10)
mf(5)
get_metadata(mongo)
Sys.sleep(10)
mf(5)
get_metadata(mongo)
```
### Redis
Launching a container with redis.
```{bash}
docker run --rm --name redisbank -d -p 6379:6379 redis:5.0.5 --requirepass bebopalula
```
```{r echo = FALSE, eval = TRUE}
system("docker run --rm --name redisbank -d -p 6379:6379 redis:5.0.5 --requirepass bebopalula")
Sys.sleep(20)
```
#### With `{memoise}`
```{r eval = TRUE}
# Create a redis cache.
# The arguments will be passed to redux::hiredis
redis_cache <- cache_redis$new(password = "bebopalula")
f <- function(x) {
sample(1:1000, x)
}
mf <- memoise(f, cache = redis_cache)
mf(5)
mf(5)
```
#### Inside `{shiny}`
Here is a first simple application that shows you the basics :
```{r}
ui <- fluidPage(
# Creating a slider input that will be used as a cache key
sliderInput("nrow", "NROW", 1, 32, 32),
# Plotting a piece of mtcars
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderCachedPlot(
{
# Pretending this takes a long time
Sys.sleep(2)
plot(mtcars[1:input$nrow, ])
},
cacheKeyExpr = list(
# Defining the cache key
input$nrow
),
# Using our redis cache
cache = redis_cache
)
}
shinyApp(ui, server)
```
For the larger app:
```{r}
generate_app(redis_cache)
```
### Postgres
Launching a container with postgres.
```{bash}
docker run --rm --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5433:5432 postgres
```
```{r echo = FALSE, eval = TRUE}
system("docker run --rm --name postgresbank -e POSTGRES_PASSWORD=mysecretpassword -d -p 5433:5432 postgres")
Sys.sleep(20)
```
#### With `{memoise}`
```{r eval = TRUE}
# Create a postgres cache.
# The arguments will be passed to DBI::dbConnect(RPostgres::Postgres(), ...)
postgres_cache <- cache_postgres$new(
dbname = "postgres",
host = "localhost",
port = 5433,
user = "postgres",
password = "mysecretpassword"
)
f <- function(x) {
sample(1:1000, x)
}
mf <- memoise(f, cache = postgres_cache)
mf(5)
mf(5)
```
#### Inside `{shiny}`
Here is a first simple application that shows you the basics :
```{r}
ui <- fluidPage(
# Creating a slider input that will be used as a cache key
sliderInput("nrow", "NROW", 1, 32, 32),
# Plotting a piece of mtcars
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderCachedPlot(
{
# Pretending this takes a long time
Sys.sleep(2)
plot(mtcars[1:input$nrow, ])
},
cacheKeyExpr = list(
# Defining the cache key
input$nrow
),
# Using our postgres cache
cache = postgres_cache
)
}
shinyApp(ui, server)
```
For the larger app:
```{r}
generate_app(postgres_cache)
```
## Chosing a cache method
### Benchmark
As we are deporting the caching to an external DBMS, the query will of course be slower than using memory cache of disk cache.
But this difference in speed comes with a simpler scalability of the caching, as several instances of the app can rely on the same caching backend without the need to be on the same machine.
```{r eval = TRUE}
library(magrittr)
library(bank)
big_iris <- purrr::rerun(100, iris) %>% data.table::rbindlist()
nrow(big_iris)
pryr::object_size(big_iris)
library(cachem)
mem_cache <- cache_mem()
disk_cache <- cache_disk()
mongo_cache <- cache_mongo$new(
db = "bank",
url = "mongodb://bebop:aloula@localhost:27066",
prefix = "sn"
)
redis_cache <- cache_redis$new(password = "bebopalula")
postgres_cache <- cache_postgres$new(
dbname = "postgres",
host = "localhost",
port = 5433,
user = "postgres",
password = "mysecretpassword"
)
bench::mark(
mem_cache = mem_cache$set("iris", big_iris),
disk_cache = disk_cache$set("iris", big_iris),
mongo_cache = mongo_cache$set("iris", big_iris),
redis_cache = redis_cache$set("iris", big_iris),
postgres_cache = postgres_cache$set("iris", big_iris),
check = FALSE,
iterations = 100
)
bench::mark(
mem_cache = mem_cache$get("iris"),
disk_cache = disk_cache$get("iris"),
mongo_cache = mongo_cache$get("iris"),
redis_cache = redis_cache$get("iris"),
postgres_cache = postgres_cache$get("iris"),
iterations = 100
)
```
```{bash}
docker stop mongobank redisbank postgresbank
```
```{r echo = FALSE, eval = TRUE}
system("docker stop mongobank redisbank postgresbank")
```
## You want another backend?
If you have any other backend in mind, feel free to open an issue here and we'll discuss the possibility of implementing it in `{bank}`.
## Code of Conduct
Please note that the bank project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.