Source/NFX/DataAccess/Cache/Table.cs
/// <summary>
/// Retrieves an item from this table by key where item age is less or equal to requested, or
///  calls the itemFactory function and inserts the value in the store
/// </summary>
/// <param name="key">Item key</param>
/// <param name="valueFactory">A function that returns new value for the specified tableName, key, and context</param>
/// <param name="factoryContext">An object to pass into the factory function if it gets invoked</param>
/// <param name="ageSec">Age of item in seconds, or 0 for any age</param>
/// <param name="putMaxAgeSec">MaxAge for item if Put is called</param>
/// <param name="putPriority">Priority for item if Put is called</param>
/// <param name="putAbsoluteExpirationUTC">Absolute expiration UTC timestamp for item if Put is called</param>
/// <typeparam name="TContext">A type of item factory context</typeparam>
Retrieves an item from this table by key where item age is less or equal to requested, or
calls the itemFactory function and inserts the value in the store
Retrieves an item from this table by key where item age is less or equal to requested or calls the itemFactory function and inserts the value in the store
public CacheRec GetOrPut<TContext>(ulong key,
                                               Func<string, ulong, TContext, object> valueFactory,
                                               TContext factoryContext = default(TContext),
                                               int ageSec = 0,
                                               int putMaxAgeSec = 0,
                                               int putPriority = 0,
                                               DateTime? putAbsoluteExpirationUTC = null)
            {
                var result = Get(key, ageSec);
                if (result != null) return result;
                var idx = getBucketIndex( key );
                var lck = m_Locks[ getLockIndex(idx) ];
                lock(lck)
                {
                    result = Get(key, ageSec); 
                    if (result!=null) return result;
                    object value = null;
                    try
                    {
                        value = valueFactory(Name, key, factoryContext);
                        Interlocked.Increment(ref stat_ValueFactoryCount);
                    }
                    catch(Exception error)
                    {
                        throw new NFXException(StringConsts.CACHE_VALUE_FACTORY_ERROR.Args("Cache.Table.GetOrPut()", error.ToMessageWithType()), error);
                    }
                    CacheRec rec;
                    this.Put(key, value, out rec, putMaxAgeSec, putPriority, putAbsoluteExpirationUTC);
                    return rec;
                }
            }
public CacheRec GetOrPut < TContext > ( ulong key , Func < string , ulong , TContext , object > valueFactory , TContext factoryContext = default ( TContext ) , int ageSec = 0 , int putMaxAgeSec = 0 , int putPriority = 0 , DateTime ? putAbsoluteExpirationUTC = null ) { var result = Get ( key , ageSec ) ; if ( result != null ) return result ; var idx = getBucketIndex ( key ) ; var lck = m_Locks [ getLockIndex ( idx ) ] ; lock ( lck ) { result = Get ( key , ageSec ) ; if ( result != null ) return result ; object value = null ; try { value = valueFactory ( Name , key , factoryContext ) ; Interlocked . Increment ( ref stat_ValueFactoryCount ) ; } catch ( Exception error ) { throw new NFXException ( StringConsts . CACHE_VALUE_FACTORY_ERROR . Args ( " " , error . ToMessageWithType ( ) ) , error ) ; } CacheRec rec ; this . Put ( key , value , out rec , putMaxAgeSec , putPriority , putAbsoluteExpirationUTC ) ; return rec ; } }
Retrieves an item from this table by key where item age is less or equal to requested, or
calls the itemFactory function and inserts the value in the store
Retrieves an item from this table by key where item age is less or equal to requested or calls the itemFactory function and inserts the value in the store
//1st check is lock-free //dbl-check under lock //lock
key
ulong
None
valueFactory
Func<string, ulong, TContext, object>
A function that returns new value for the specified tableName, key, and context
A function that returns new value for the specified tableName key and context
factoryContext
TContext
An object to pass into the factory function if it gets invoked
An object to pass into the factory function if it gets invoked
ageSec
int
Age of item in seconds, or 0 for any age
Age of item in seconds or 0 for any age
putMaxAgeSec
int
MaxAge for item if Put is called
MaxAge for item if Put is called
putPriority
int
Priority for item if Put is called
Priority for item if Put is called
putAbsoluteExpirationUTC
DateTime?
Absolute expiration UTC timestamp for item if Put is called
Absolute expiration UTC timestamp for item if Put is called
typeparam
A type of item factory context
A type of item factory context