cache java

cache java,第1张

cache java是什么, 让我们一起了解一下?

Cache 是一个像 Map 一样的数据结构,它允许基于 Key 的临时储存。缓存被单个 CacheManager 拥有。

Java 的缓存 API 定义了五个核心接口:CachingProvider,CacheManager,Cache,Entry 和 ExpiryPolicy。

Java实现cache的基本机制是什么?

我这里说的cache不是指CPU和RAM之间的缓存,而是java应用中间常用的缓存。最常使用的场合就是访问数据库的时候为了提高效率而使用的 cache。一般的用法就是把数据从数据库读到内存,然后之后的数据访问都从内存来读,从而减少对数据库的读取次数来提高效率。

说了这么多,Java 下到底如何实现Cache,希望下面的实际案例可以帮助到你。 public class CacheFactory {    private static ConcurrentHashMap  caches = new ConcurrentHashMap()    private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1)      private static void register(Cache cache) {       caches.put(cache.category(), cache)    }      private static void registerAll() {       register(new StockCache())    }      public static void init() {       registerAll()         for (Cache cache : caches.values()) {          executorService.scheduleAtFixedRate(new Runnable() {             @Override             public void run() {                cache.refresh()             }          }, 0, cache.interval(), TimeUnit.MILLISECONDS)       }    }      public static Cache getCache(String key) {       if (caches.contains(key)) {          return caches.get(key)       }       return null    } }   // cache接口除了需要提供interval和refresh以外,还需要提供一个category来区分不同的Cache public interface Cache {    /**     * Refresh the cache. If succeed, return true, else return false     *      * @return     */    boolean refresh()      /**     * How much time it will refresh the cache.     *      * @return     */    long interval()      /**     * Cache's category. Each cache has distinct category.     *      * @return     */    String category() }

java缓存技术

一、什么是缓存

1、Cache是高速缓冲存储器

一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问

2、凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为

Cache

二、缓存的分类

1、基于web应用的系统架构图

2、在系统架构的不同层级之间,为了加快访问速度,都可以存在缓存

*** 作系统磁盘缓存->减少磁盘机械 *** 作

数据库缓存->减少文件系统I/O

应用程序缓存->减少对数据库的查询

Web服务器缓存->减少应用服务器请求

客户端浏览器缓存->减少对网站的访问。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8111229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存