Exemplo n.º 1
0
    public String flushOrder() {
      StrWriter out = new StrWriter();

      for (CacheElement c : values()) {
        if (c.isDirty()) out.print("<" + c.getAddress().get() + ">");
      }

      return out.toString();
    }
Exemplo n.º 2
0
  private void setInt32(long offset, int value) throws IOException {
    long addr = (long) (offset / elementSize);
    int ofs = (int) (offset % elementSize);

    CacheElement c = get(addr);
    byte[] data = c.getData();

    LittleEndian.setInt32(data, ofs, value);

    c.setDirty();
  }
Exemplo n.º 3
0
  public Object get(Object key) {
    CacheElement cacheElement = storeMap.get(key);
    // 数据已经过去
    if (cacheElement == null) {
      return null;
    }

    if (System.currentTimeMillis() - cacheElement.getExpiredTime() > 0) {
      removeLocal(key);
      return null;
    }
    return cacheElement.getValue();
  }
Exemplo n.º 4
0
  private CacheElement put(long address) throws IOException {
    /** get a CacheElement from the stack object pool */
    CacheElement c = map.pop();

    /** read the element from the device */
    c.read(address);

    /** and insert the element into the LinkedHashMap */
    map.put(c);

    /**
     * stack "must" contains at least one entry the placeholder ... so let it throw an exception if
     * this is false
     */
    CacheElement e = map.peek();
    // if an element was discarded from the LRU cache
    // now we can free it ... this will send the element
    // to storage if is marked as dirty
    if (!e.isFree()) e.free();

    return c;
  }
Exemplo n.º 5
0
 @SuppressWarnings("unchecked")
 /**
  * get a key. if varArgs is not null these values will be passed to the serializer in the case
  * that the cache object does not exist
  *
  * @param key cache key
  * @param varArgs arguments that will be passed to serializer when a key is not found
  * @return
  */
 public Value get(Key key, Object... varArgs) {
   if (DEBUG) System.out.println(" - getting key = " + key + " contains=" + map.containsKey(key));
   CacheElement o = map.get(key);
   //		UniqueKey<Key> t = map.get(key);
   if (o == null) {
     MutableBoolean isdirty = new MutableBoolean(false);
     UniqueKey<Key> t = serializer.load(key, isdirty, varArgs);
     if (DEBUG) System.out.println("  - loaded element  = " + t + " ");
     if (t == null) return null;
     o = new CacheElement(t);
     key = t.getKey();
     synchronized (map) {
       map.put(key, o);
     }
     if (DEBUG)
       System.out.println(
           "  - adding key = " + key + " contains=" + map.containsKey(key) + "  dirty=" + dirty);
     if (isdirty.booleanValue()) { // / If its dirty, add to our dirty set
       synchronized (dirty) {
         dirty.add(key);
       }
     }
   }
   o.setUsed();
   if (autoFlush && autoFlushTime != null) {
     flushOld(autoFlushTime);
   }
   return (Value) o.v;
 }
Exemplo n.º 6
0
 private CacheElement put(CacheElement c) {
   return put(c.getAddress(), c);
 }
Exemplo n.º 7
0
 public void flush() throws IOException {
   for (CacheElement c : map.values()) {
     c.flush();
   }
 }
Exemplo n.º 8
0
 public void flush(long address) throws IOException {
   CacheElement c = map.get(address);
   if (c != null) c.flush();
 }