在 R studio 上运行R代码时,公共区域的变量会存在内存中且可以被全局访问 (私有代码块内的临时变量除外),这和Python很像,和静态语言如JAVA, C 很不一样。
R/python进行数据分析过程中,需要刻意用到缓存的场景倒不是很多,一般的公共区域变量全局访问就够用了。但在大一些的项目中,譬如我写了一个提供 动态生成models,计算统计指标,画图等APIs 的R服务器,这时候缓存一下请求结果,就不用每次重新计算了。
我尝试自己写个list,用key-value的形式模仿实现Java中的map,在服务器端却出现了奇怪的并发bug。。这里提供一个可以实现“缓存任意R对象”的R包。
With this package【1】, any R object can be cached in a key-value storage where the key can be an arbitrary set of R objects. The cache memory is persistent (on the file system).
清空缓存:getCacheRootPath()
Gets the root path to the file cache directory
This directory structure was created by the R package 'R.cache'
available on CRAN [https://cran.r-project.org/package=R.cache].
It holds cache files containing results memoized by various
R packages that utilize the R.cache package. It is safe to
delete all o