Comparison:
LruRedux::ThreadSafeCache: 337353.5 i/s
ActiveSupport::Cache::MemoryStore: 52808.1 i/s - 6.39x slower
ActiveSupport::Cache::FileStore: 12341.5 i/s - 27.33x slower
ActiveSupport::Cache::DalliStore: 6629.1 i/s - 50.89x slower
ActiveSupport::Cache::RedisStore: 6304.6 i/s - 53.51x slower
ActiveSupport::Cache::DalliStore at pub-memcache-13640.us-east-1-1.2.ec2.garantiadata.com:13640: 26.9 i/s - 12545.27x slower
ActiveSupport::Cache::RedisStore at pub-redis-11469.us-east-1-4.2.ec2.garantiadata.com: 25.8 i/s - 13062.87x slower
@billy @lgn21st @kgen
但是这是这篇博客的结果,单机上FileStore甚至比memcached和redis快,当时也是注意到这点。
文章里面关于每个缓存的优势和劣势都讲解的比较清楚,如果只是单机的话:
FileStore:
Advantages:
- Disk space is cheaper than RAM
Disadvantages:
- Not an LRU cache
When should I use ActiveSupport::FileStore?
Reach for FileStore if you have low request load (1 or 2 servers) and still need a very large cache (>100MB). Also, don't use it on Heroku.
Memcache and dalli:
Disadvantages:
- Expensive.
- Cache values are limited to 1MB.
Redis and redis-store:
Advantages:
- Allows different eviction policies beyond LRU
- Can persist to disk, allowing hot restarts
Disadvantages:
- Expensive
- While Redis supports several data types, redis-store only supports Strings
LRURedux:
Advantages:
Disadvantages:
- Caches can't be shared across processes or hosts
- Caches add to your total RAM usage
- Can't use it as a Rails cache store
其实Memcache/Redis,对比来看,两个无论是性能还是特性在单机上都差不是太多,甚至有网络访问,需要多机器是也差不多。相比而言Redis还有比较好的缓存实效时间的设置和热备份机制。所以这两个来选可以选Redis。
对比FileStore来看,虽然从理论上讲访问硬盘肯定要比访问内存慢,但是文章中的测试结果是,访问文件系统更快,这点我也比较有疑问。不过比较好的地方就是FileStore比较廉价,不好的地方就是没有LRU。
如果楼主有比较大的内容需要缓存并且内存吃紧,可以用FileStore缓存来处理,但是记得自己清除缓存就好了。Redis的话,比较方便的是缓存的生命周期可以设置。
其实我觉得这个LRURedux也不错,这变态的性能也让人跃跃欲试。不过就是缺点也挺多,Caches can't be shared across processes or hosts,感觉有点像MemoryStore。
楼主可以FileStore做大文件的缓存,然后同时可以用Redis-objects存一些数据,然后同时使用LRURedux来做一些小片段的缓存,要注意的是这货和MemoryStore一样不能processes之间共享。😄