当前位置: 移动技术网 > 移动技术>移动开发>Android > Java和Android的LRU缓存及实现原理

Java和Android的LRU缓存及实现原理

2019年07月24日  | 移动技术网移动技术  | 我要评论
一、概述 android提供了lrucache类,可以方便的使用它来实现lru算法的缓存。java提供了linkedhashmap,可以用该类很方便的实现lru算法,ja

一、概述

android提供了lrucache类,可以方便的使用它来实现lru算法的缓存。java提供了linkedhashmap,可以用该类很方便的实现lru算法,java的lrulinkedhashmap就是直接继承了linkedhashmap,进行了极少的改动后就可以实现lru算法。

二、java的lru算法

java的lru算法的基础是linkedhashmap,linkedhashmap继承了hashmap,并且在hashmap的基础上进行了一定的改动,以实现lru算法。

1、hashmap

首先需要说明的是,hashmap将每一个节点信息存储在entry<k,v>结构中。entry<k,v>中存储了节点对应的key、value、hash信息,同时存储了当前节点的下一个节点的引用。因此entry<k,v>是一个单向链表。hashmap的存储结构是一个数组加单向链表的形式。每一个key对应的hashcode,在hashmap的数组中都可以找到一个位置;而如果多个key对应了相同的hashcode,那么他们在数组中对应在相同的位置上,这时,hashmap将把对应的信息放到entry<k,v>中,并使用链表连接这些entry<k,v>。

 static class entry<k,v> implements map.entry<k,v> {
    final k key;
    v value;
    entry<k,v> next;
    int hash;

    /**
     * creates new entry.
     */
    entry(int h, k k, v v, entry<k,v> n) {
      value = v;
      next = n;
      key = k;
      hash = h;
    }

    public final k getkey() {
      return key;
    }

    public final v getvalue() {
      return value;
    }

    public final v setvalue(v newvalue) {
      v oldvalue = value;
      value = newvalue;
      return oldvalue;
    }

    public final boolean equals(object o) {
      if (!(o instanceof map.entry))
        return false;
      map.entry e = (map.entry)o;
      object k1 = getkey();
      object k2 = e.getkey();
      if (k1 == k2 || (k1 != null && k1.equals(k2))) {
        object v1 = getvalue();
        object v2 = e.getvalue();
        if (v1 == v2 || (v1 != null && v1.equals(v2)))
          return true;
      }
      return false;
    }

    public final int hashcode() {
      return objects.hashcode(getkey()) ^ objects.hashcode(getvalue());
    }

    public final string tostring() {
      return getkey() + "=" + getvalue();
    }

    /**
     * this method is invoked whenever the value in an entry is
     * overwritten by an invocation of put(k,v) for a key k that's already
     * in the hashmap.
     */
    void recordaccess(hashmap<k,v> m) {
    }

    /**
     * this method is invoked whenever the entry is
     * removed from the table.
     */
    void recordremoval(hashmap<k,v> m) {
    }
  }

   下面贴一下hashmap的put方法的代码,并进行分析

public v put(k key, v value) {
    if (table == empty_table) {
      inflatetable(threshold);
    }
    if (key == null)
      return putfornullkey(value);

     //以上信息不关心,下面是正常的插入逻辑。

     //首先计算hashcode
    int hash = hash(key);
     //通过计算得到的hashcode,计算出hashcode在数组中的位置
    int i = indexfor(hash, table.length);

     //for循环,找到在hashmap中是否存在一个节点,对应的key与传入的key完全一致。如果存在,说明用户想要替换该key对应的value值,因此直接替换value即可返回。
    for (entry<k,v> e = table[i]; e != null; e = e.next) {
      object k;
      if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
        v oldvalue = e.value;
        e.value = value;
        e.recordaccess(this);
        return oldvalue;
      }
    }

     //逻辑执行到此处,说明hashmap中不存在完全一致的kye.调用addentry,新建一个节点保存key、value信息,并增加到hashmap中
    modcount++;
    addentry(hash, key, value, i);
    return null;
  }

  在上面的代码中增加了一些注释,可以对整体有一个了解。下面具体对一些值得分析的点进行说明。

<1> int i = indexfor(hash, table.length);

可以看一下源码:

 static int indexfor(int h, int length) {
    // assert integer.bitcount(length) == 1 : "length must be a non-zero power of 2";
    return h & (length-1);
  }

 为什么获得的hashcode(h)要和(length-1)进行按位与运算?这是为了保证去除掉h的高位信息。如果数组大小为8(1000),而计算出的h的值为10(1010),如果直接获取数组的index为10的数据,肯定会抛出数组超出界限异常。所以使用按位与(0111&1010),成功清除掉高位信息,得到2(0010),表示对应数组中index为2的数据。效果与取余相同,但是位运算的效率明显更高。

但是这样有一个问题,如果length为9,获取得length-1信息为8(1000),这样进行位运算,不但不能清除高位数据,得到的结果肯定不对。所以数组的大小一定有什么特别的地方。通过查看源码,可以发现,hashmap无时无刻不在保证对应的数组个数为2的n次方。

首先在put的时候,调用inflatetable方法。重点在于rounduptopowerof2方法,虽然它的内容包含大量的位相关的运算和处理,没有看的很明白,但是注释已经明确了,会保证数组的个数为2的n次方。

private void inflatetable(int tosize) {
// find a power of 2 >= tosize
int capacity = rounduptopowerof2(tosize);

threshold = (int) math.min(capacity * loadfactor, maximum_capacity + 1);
table = new entry[capacity];
inithashseedasneeded(capacity);
}

其次,在addentry等其他位置,也会使用(2 * table.length)、table.length << 1等方式,保证数组的个数为2的n次方。

<2> for (entry<k,v> e = table[i]; e != null; e = e.next)

因为hashmap使用的是数组加链表的形式,所以通过hashcode获取到在数组中的位置后,得到的不是一个entry<k,v>,而是一个entry<k,v>的链表,一定要循环链表,获取key对应的value。

<3> addentry(hash, key, value, i);

先判断数组个数是否超出阈值,如果超过,需要增加数组个数。然后会新建一个entry,并加到数组中。

/**
   * adds a new entry with the specified key, value and hash code to
   * the specified bucket. it is the responsibility of this
   * method to resize the table if appropriate.
   *
   * subclass overrides this to alter the behavior of put method.
   */
  void addentry(int hash, k key, v value, int bucketindex) {
    if ((size >= threshold) && (null != table[bucketindex])) {
      resize(2 * table.length);
      hash = (null != key) ? hash(key) : 0;
      bucketindex = indexfor(hash, table.length);
    }

    createentry(hash, key, value, bucketindex);
  }

  /**
   * like addentry except that this version is used when creating entries
   * as part of map construction or "pseudo-construction" (cloning,
   * deserialization). this version needn't worry about resizing the table.
   *
   * subclass overrides this to alter the behavior of hashmap(map),
   * clone, and readobject.
   */
  void createentry(int hash, k key, v value, int bucketindex) {
    entry<k,v> e = table[bucketindex];
    table[bucketindex] = new entry<>(hash, key, value, e);
    size++;
  }

    2、linkedhashmap

linkedhashmap在hashmap的基础上,进行了修改。首先将entry由单向链表改成双向链表。增加了before和after两个队entry的引用。

 private static class entry<k,v> extends hashmap.entry<k,v> {
    // these fields comprise the doubly linked list used for iteration.
    entry<k,v> before, after;

    entry(int hash, k key, v value, hashmap.entry<k,v> next) {
      super(hash, key, value, next);
    }

    /**
     * removes this entry from the linked list.
     */
    private void remove() {
      before.after = after;
      after.before = before;
    }

    /**
     * inserts this entry before the specified existing entry in the list.
     */
    private void addbefore(entry<k,v> existingentry) {
      after = existingentry;
      before = existingentry.before;
      before.after = this;
      after.before = this;
    }

    /**
     * this method is invoked by the superclass whenever the value
     * of a pre-existing entry is read by map.get or modified by map.set.
     * if the enclosing map is access-ordered, it moves the entry
     * to the end of the list; otherwise, it does nothing.
     */
    void recordaccess(hashmap<k,v> m) {
      linkedhashmap<k,v> lm = (linkedhashmap<k,v>)m;
      if (lm.accessorder) {
        lm.modcount++;
        remove();
        addbefore(lm.header);
      }
    }

    void recordremoval(hashmap<k,v> m) {
      remove();
    }
  }

  同时,linkedhashmap提供了一个对entry的引用header(private transient entry<k,v> header)。header的作用就是永远只是hashmap中所有成员的头(header.after)和尾(header.before)。这样把hashmap本身的数组加链表的格式进行了修改。在linkedhashmap中,即保留了hashmap的数组加链表的数据保存格式,同时增加了一套header作为开始标记的双向链表(我们暂且称之为header的双向链表)。linkedhashmap就是通过header的双向链表来实现lru算法的。header.after永远指向最近最不常使用的那个节点,删除的话,就是删除这个header.after对应的节点。相对的,header.before指向的就是刚刚使用过的那个节点。

linkedhashmap并没有提供put方法,但是linkedhashmap重写了addentry和createentry方法,如下:

 /**
   * this override alters behavior of superclass put method. it causes newly
   * allocated entry to get inserted at the end of the linked list and
   * removes the eldest entry if appropriate.
   */
  void addentry(int hash, k key, v value, int bucketindex) {
    super.addentry(hash, key, value, bucketindex);

    // remove eldest entry if instructed
    entry<k,v> eldest = header.after;
    if (removeeldestentry(eldest)) {
      removeentryforkey(eldest.key);
    }
  }

  /**
   * this override differs from addentry in that it doesn't resize the
   * table or remove the eldest entry.
   */
  void createentry(int hash, k key, v value, int bucketindex) {
    hashmap.entry<k,v> old = table[bucketindex];
    entry<k,v> e = new entry<>(hash, key, value, old);
    table[bucketindex] = e;
    e.addbefore(header);
    size++;
  }

   hashmap的put方法,调用了addentry方法;hashmap的addentry方法又调用了createentry方法。因此可以把上面的两个方法和hashmap中的内容放到一起,方便分析,形成如下方法:

 void addentry(int hash, k key, v value, int bucketindex) {
    if ((size >= threshold) && (null != table[bucketindex])) {
      resize(2 * table.length);
      hash = (null != key) ? hash(key) : 0;
      bucketindex = indexfor(hash, table.length);
    }

    hashmap.entry<k,v> old = table[bucketindex];
    entry<k,v> e = new entry<>(hash, key, value, old);
    table[bucketindex] = e;
    e.addbefore(header);
    size++;

    // remove eldest entry if instructed
    entry<k,v> eldest = header.after;
    if (removeeldestentry(eldest)) {
      removeentryforkey(eldest.key);
    }
  }

 同样,先判断是否超出阈值,超出则增加数组的个数。然后创建entry对象,并加入到hashmap对应的数组和链表中。与hashmap不同的是linkedhashmap增加了e.addbefore(header);和removeentryforkey(eldest.key);这样两个操作。

首先分析一下e.addbefore(header)。其中e是linkedhashmap.entry对象,addbefore代码如下,作用就是讲header与当前对象相关联,使当前对象增加到header的双向链表的尾部(header.before):

  private void addbefore(entry<k,v> existingentry) {
      after = existingentry;
      before = existingentry.before;
      before.after = this;
      after.before = this;
    }

  其次是另一个重点,代码如下:

  // remove eldest entry if instructed
    entry<k,v> eldest = header.after;
    if (removeeldestentry(eldest)) {
      removeentryforkey(eldest.key);
    }

     其中,removeeldestentry判断是否需要删除最近最不常使用的那个节点。linkedhashmap中的removeeldestentry(eldest)方法永远返回false,如果我们要实现lru算法,就需要重写这个方法,判断在什么情况下,删除最近最不常使用的节点。removeentryforkey的作用就是将key对应的节点在hashmap的数组加链表结构中删除,源码如下:

 final entry<k,v> removeentryforkey(object key) {
    if (size == 0) {
      return null;
    }
    int hash = (key == null) ? 0 : hash(key);
    int i = indexfor(hash, table.length);
    entry<k,v> prev = table[i];
    entry<k,v> e = prev;

    while (e != null) {
      entry<k,v> next = e.next;
      object k;
      if (e.hash == hash &&
        ((k = e.key) == key || (key != null && key.equals(k)))) {
        modcount++;
        size--;
        if (prev == e)
          table[i] = next;
        else
          prev.next = next;
        e.recordremoval(this);
        return e;
      }
      prev = e;
      e = next;
    }

    return e;
  }

 removeentryforkey是hashmap的方法,对linkedhashmap中header的双向链表无能为力,而linkedhashmap又没有重写这个方法,那header的双向链表要如何处理呢。

仔细看一下代码,可以看到在成功删除了hashmap中的节点后,调用了e.recordremoval(this);方法。这个方法在hashmap中为空,linkedhashmap的entry则实现了这个方法。其中remove()方法中的两行代码为双向链表中删除当前节点的标准代码,不解释。

 /**
     * removes this entry from the linked list.
     */
    private void remove() {
      before.after = after;
      after.before = before;
    }void recordremoval(hashmap<k,v> m) {
      remove();
    }

      以上,linkedhashmap增加节点的代码分析完毕,可以看到完美的将新增的节点放在了header双向链表的末尾。

但是,这样显然是先进先出的算法,而不是最近最不常使用算法。需要在get的时候,更新header双向链表,把刚刚get的节点放到header双向链表的末尾。我们来看看get的源码:

public v get(object key) {
    entry<k,v> e = (entry<k,v>)getentry(key);
    if (e == null)
      return null;
    e.recordaccess(this);
    return e.value;
  }

  代码很短,第一行的getentry调用的是hashmap的getentry方法,不需要解释。真正处理header双向链表的代码是e.recordaccess(this)。看一下代码:

   /**
     * removes this entry from the linked list.
     */
    private void remove() {
      before.after = after;
      after.before = before;
    }

    /**
     * inserts this entry before the specified existing entry in the list.
     */
    private void addbefore(entry<k,v> existingentry) {
      after = existingentry;
      before = existingentry.before;
      before.after = this;
      after.before = this;
    }

    /**
     * this method is invoked by the superclass whenever the value
     * of a pre-existing entry is read by map.get or modified by map.set.
     * if the enclosing map is access-ordered, it moves the entry
     * to the end of the list; otherwise, it does nothing.
     */
    void recordaccess(hashmap<k,v> m) {
      linkedhashmap<k,v> lm = (linkedhashmap<k,v>)m;
      if (lm.accessorder) {
        lm.modcount++;
        remove();
        addbefore(lm.header);
      }
    }

  首先在header双向链表中删除当前节点,再将当前节点添加到header双向链表的末尾。当然,在调用linkedhashmap的时候,需要将accessorder设置为true,否则就是fifo算法。

三、android的lru算法

android同样提供了hashmap和linkedhashmap,而且总体思路有些类似,但是实现的细节明显不同。而且android提供的lrucache虽然使用了linkedhashmap,但是实现的思路并不一样。java需要重写removeeldestentry来判断是否删除节点;而android需要重写lrucache的sizeof,返回当前节点的大小,android会根据这个大小判断是否超出了限制,进行调用trimtosize方法清除多余的节点。

android的sizeof方法默认返回1,默认的方式是判断hashmap中的数据个数是否超出了设置的阈值。也可以重写sizeof方法,返回当前节点的大小。android的safesizeof会调用sizeof方法,其他判断阈值的方法会调用safesizeof方法,进行加减操作并判断阈值。进而判断是否需要清除节点。

java的removeeldestentry方法,也可以达到同样的效果。java需要使用者自己提供整个判断的过程,两者思路还是有些区别的。

sizeof,safesizeof不需要说明,而put和get方法,虽然和java的实现方式不完全一样,但是思路是相同的,也不需要分析。在lrucache中put方法的最后,会调用trimtosize方法,这个方法用于清除超出的节点。它的代码如下:

 public void trimtosize(int maxsize)
 {
  while (true)
  {
   object key;
   object value;
   synchronized (this) {
    if ((this.size < 0) || ((this.map.isempty()) && (this.size != 0))) {
     throw new illegalstateexception(getclass().getname() + ".sizeof() is reporting inconsistent results!");
    }
      if (size <= maxsize) {
        break;
      }

    map.entry toevict = (map.entry)this.map.entryset().iterator().next();

    key = toevict.getkey();
    value = toevict.getvalue();
    this.map.remove(key);
    this.size -= safesizeof(key, value);
    this.evictioncount += 1;
   }

   entryremoved(true, key, value, null);
  }
 }

 重点需要说明的是map.entry toevict = (map.entry)this.map.entryset().iterator().next();这行代码。它前面的代码判断是否需要删除最近最不常使用的节点,后面的代码用于删除具体的节点。这行代码用于获取最近最不常使用的节点。

首先需要说明的问题是,android的linkedhashmap和java的linkedhashmap在思路上一样,也是使用header保存双向链表。在put和get的时候,会更新对应的节点,保存header.after指向最久没有使用的节点;header.before用于指向刚刚使用过的节点。所以map.entry toevict = (map.entry)this.map.entryset().iterator().next();这行最终肯定是获取header.after节点。下面逐步分析代码,就可以看到是如何实现的了。

首先,map.entryset(),hashmap定义了这个方法,linkedhashmap没有重写这个方法。因此调用的是hashmap对应的方法:

public set<entry<k, v>> entryset() {
    set<entry<k, v>> es = entryset;
    return (es != null) ? es : (entryset = new entryset());
  }

上面代码不需要细说,new一个entryset类的实例。而entryset也是在hashmap中定义,linkedhashmap中没有。

private final class entryset extends abstractset<entry<k, v>> {
    public iterator<entry<k, v>> iterator() {
      return newentryiterator();
    }
    public boolean contains(object o) {
      if (!(o instanceof entry))
        return false;
      entry<?, ?> e = (entry<?, ?>) o;
      return containsmapping(e.getkey(), e.getvalue());
    }
    public boolean remove(object o) {
      if (!(o instanceof entry))
        return false;
      entry<?, ?> e = (entry<?, ?>)o;
      return removemapping(e.getkey(), e.getvalue());
    }
    public int size() {
      return size;
    }
    public boolean isempty() {
      return size == 0;
    }
    public void clear() {
      hashmap.this.clear();
    }
  }

  iterator<entry<k, v>> newentryiterator() { return new entryiterator(); }

代码中很明显的可以看出,map.entry toevict = (map.entry)this.map.entryset().iterator().next(),就是要调用

newentryiterator().next(),就是调用(new entryiterator()).next()。而entryiterator类在linkedhashmap中是有定义的。

  private final class entryiterator
      extends linkedhashiterator<map.entry<k, v>> {
    public final map.entry<k, v> next() { return nextentry(); }
  }

  private abstract class linkedhashiterator<t> implements iterator<t> {
    linkedentry<k, v> next = header.nxt;
    linkedentry<k, v> lastreturned = null;
    int expectedmodcount = modcount;

    public final boolean hasnext() {
      return next != header;
    }

    final linkedentry<k, v> nextentry() {
      if (modcount != expectedmodcount)
        throw new concurrentmodificationexception();
      linkedentry<k, v> e = next;
      if (e == header)
        throw new nosuchelementexception();
      next = e.nxt;
      return lastreturned = e;
    }

    public final void remove() {
      if (modcount != expectedmodcount)
        throw new concurrentmodificationexception();
      if (lastreturned == null)
        throw new illegalstateexception();
      linkedhashmap.this.remove(lastreturned.key);
      lastreturned = null;
      expectedmodcount = modcount;
    }
  }

现在可以得到结论,trimtosize中的那行代码得到的就是header.next对应的节点,也就是最近最不常使用的那个节点。

以上就是对android java lru缓存的实现原理做的详解,后续继续补充相关资料,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网