Class AttributeCache

  • All Implemented Interfaces:

    
    public class AttributeCache<K, V>
    
                        

    thread safe

    Since:

    2023-11-02

    Author:

    trydofor

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      Class<out Object> getOwner()
      TypedReg<K, V> getTyped()
      String getName()
      int getSize()
      int getLive()
      int getIdle()
      void putAttr(@NotNull() K key, @NotNull() V value) Put key and value
      void putAttr(@NotNull() K key, @NotNull() V value, int ttl) Put key and value with ttl
      void putAttrs(@NotNull() Map<K, V> map) Put keys and values
      void putAttrs(@NotNull() Map<K, V> map, int ttl) Put keys and values with ttl
      V tryAttr(@NotNull() K key, V elze) Try to get value by key, load it if not found, return `elze` if the result is null.
      V tryAttr(@NotNull() K key, V elze, int ttl) Try to get value by key, load it if not found, return `elze` if the result is null.
      V tryAttr(@NotNull() K key) Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.
      V tryAttr(@NotNull() K key, int ttl) Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.
      V tryAttr(@NotNull() K key, boolean notnull) Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.
      V tryAttr(@NotNull() K key, boolean notnull, int ttl) Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.
      V getAttr(@NotNull() K key) Get an attribute by typed key, and NOT load if not found.
      Map<K, V> getAttrs(@NotNull() Collection<K> key) Get all attributes by typed keys, and NOT load if not found.
      void ridAttr(K key) remove an attribute by key
      void ridAttrs(Collection<out K> key) remove all attribute by keys
      void ridAttrAll() remove all attribute
      void register() register to the global
      void unregister() unregister from the global
      static Set<TypedReg<out Object, out Object>> registered() thread safely list the registered types
      static <K, V> void forEach(@NotNull() TypedReg<K, V> reg, @NotNull() Consumer<AttributeCache<K, V>> handle) thread safely loop the registered type
      void setLoader(Function<K, V> loader)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AttributeCache

        AttributeCache(Class<out Object> owner, TypedReg<K, V> reg, int max, int ttl, int tti)
      • AttributeCache

        AttributeCache(Class<out Object> owner, TypedReg<K, V> reg, int max, int ttl, int tti, Function<K, V> loader)
    • Method Detail

      • putAttr

         void putAttr(@NotNull() K key, @NotNull() V value)

        Put key and value

        Parameters:
        key - the key
        value - the value
      • putAttr

         void putAttr(@NotNull() K key, @NotNull() V value, int ttl)

        Put key and value with ttl

        Parameters:
        key - the key
        value - the value
        ttl - time to live in second
      • putAttrs

         void putAttrs(@NotNull() Map<K, V> map)

        Put keys and values

        Parameters:
        map - map of key-value
      • putAttrs

         void putAttrs(@NotNull() Map<K, V> map, int ttl)

        Put keys and values with ttl

        Parameters:
        map - map of key-value
        ttl - time to live in second
      • tryAttr

        @Contract(value = "_,!null->!null") V tryAttr(@NotNull() K key, V elze)

        Try to get value by key, load it if not found, return `elze` if the result is null.

        Parameters:
        key - unique key, e.g.
        elze - return `elze` if result is null
      • tryAttr

        @Contract(value = "_,!null,_->!null") V tryAttr(@NotNull() K key, V elze, int ttl)

        Try to get value by key, load it if not found, return `elze` if the result is null.

        Parameters:
        key - unique key, e.g.
        elze - return `elze` if result is null
        ttl - ttl in second
      • tryAttr

        @NotNull() V tryAttr(@NotNull() K key)

        Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.

        Parameters:
        key - unique key, e.g.
      • tryAttr

        @NotNull() V tryAttr(@NotNull() K key, int ttl)

        Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.

        Parameters:
        key - unique key, e.g.
        ttl - ttl in second
      • tryAttr

        @Contract(value = "_,true ->!null") V tryAttr(@NotNull() K key, boolean notnull)

        Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.

        Parameters:
        key - unique key, e.g.
        notnull - whether notnull
      • tryAttr

        @Contract(value = "_,true,_ ->!null") V tryAttr(@NotNull() K key, boolean notnull, int ttl)

        Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.

        Parameters:
        key - unique key, e.g.
        notnull - whether notnull
        ttl - ttl in second
      • getAttr

        @Nullable() V getAttr(@NotNull() K key)

        Get an attribute by typed key, and NOT load if not found.

        Parameters:
        key - unique key, e.g.
      • getAttrs

        @NotNull() Map<K, V> getAttrs(@NotNull() Collection<K> key)

        Get all attributes by typed keys, and NOT load if not found.

        Parameters:
        key - unique key, e.g.
      • ridAttr

         void ridAttr(K key)

        remove an attribute by key

        Parameters:
        key - unique key, e.g.
      • ridAttrs

         void ridAttrs(Collection<out K> key)

        remove all attribute by keys

        Parameters:
        key - unique key, e.g.
      • ridAttrAll

         void ridAttrAll()

        remove all attribute

      • register

         void register()

        register to the global

      • unregister

         void unregister()

        unregister from the global