Package net.i2p.data

Class SDSCache<V extends SimpleDataStructure>


  • public class SDSCache<V extends SimpleDataStructure>
    extends Object
    A least recently used cache with a max size, for SimpleDataStructures. The index to the cache is the first 4 bytes of the data, so the data must be sufficiently random. This caches the SDS objects, and also uses SimpleByteCache to cache the unused byte arrays themselves Following is sample usage:
    
        private static final SDSCache<Foo> _cache = new SDSCache(Foo.class, LENGTH, 1024);
    
        public static Foo create(byte[] data) {
            return _cache.get(data);
        }
    
        public static Foo create(byte[] data, int off) {
            return _cache.get(data, off);
        }
    
        public static Foo create(InputStream in) throws IOException {
            return _cache.get(in);
        }
    
      
    Since:
    0.8.3
    Author:
    zzz
    • Constructor Detail

      • SDSCache

        public SDSCache​(Class<V> rvClass,
                        int len,
                        int max)
        Parameters:
        rvClass - the class that we are storing, i.e. an extension of SimpleDataStructure
        len - the length of the byte array in the SimpleDataStructure
        max - maximum size of the cache assuming 128MB of mem. The actual max size will be scaled based on available memory.
    • Method Detail

      • clear

        public void clear()
        Since:
        0.9.17
      • get

        public V get​(byte[] data)
        WARNING - If the SDS is found in the cache, the passed-in byte array will be returned to the SimpleByteCache for reuse. Do NOT save a reference to the passed-in data, or use or modify it, after this call.
        Parameters:
        data - non-null, the byte array for the SimpleDataStructure
        Returns:
        the cached value if available, otherwise makes a new object and returns it
        Throws:
        IllegalArgumentException - if data is not the correct number of bytes
        NullPointerException
      • get

        public V get​(byte[] b,
                     int off)