예제 #1
0
        /// <summary>
        /// Do not add an item to the cache unless the current transaction
        /// timestamp is later than the timestamp at which the item was
        /// invalidated. (Otherwise, a stale item might be re-added if the
        /// database is operating in repeatable read isolation mode.)
        /// </summary>
        /// <returns>Whether the item was actually put into the cache</returns>
        public async Task <bool> PutAsync(CacheKey key, object value, long txTimestamp, object version, IComparer versionComparator,
                                          bool minimalPut, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (txTimestamp == long.MinValue)
            {
                // MinValue means cache is disabled
                return(false);
            }

            using (await _lockObjectAsync.LockAsync())
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug("Caching: {0}", key);
                }

                try
                {
                    await(cache.LockAsync(key, cancellationToken)).ConfigureAwait(false);

                    ILockable lockable = (ILockable)await(cache.GetAsync(key, cancellationToken)).ConfigureAwait(false);

                    bool puttable = lockable == null ||
                                    lockable.IsPuttable(txTimestamp, version, versionComparator);

                    if (puttable)
                    {
                        await(cache.PutAsync(key, new CachedItem(value, cache.NextTimestamp(), version), cancellationToken)).ConfigureAwait(false);
                        if (log.IsDebugEnabled())
                        {
                            log.Debug("Cached: {0}", key);
                        }
                        return(true);
                    }
                    else
                    {
                        if (log.IsDebugEnabled())
                        {
                            if (lockable.IsLock)
                            {
                                log.Debug("Item was locked: {0}", key);
                            }
                            else
                            {
                                log.Debug("Item was already cached: {0}", key);
                            }
                        }
                        return(false);
                    }
                }
                finally
                {
                    await(cache.UnlockAsync(key, cancellationToken)).ConfigureAwait(false);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Do not add an item to the cache unless the current transaction
        /// timestamp is later than the timestamp at which the item was
        /// invalidated. (Otherwise, a stale item might be re-added if the
        /// database is operating in repeatable read isolation mode.)
        /// </summary>
        /// <returns>Whether the item was actually put into the cache</returns>
        public bool Put(CacheKey key, object value, long txTimestamp, object version, IComparer versionComparator,
                        bool minimalPut)
        {
            if (txTimestamp == long.MinValue)
            {
                // MinValue means cache is disabled
                return(false);
            }

            lock (_lockObject)
            {
                if (log.IsDebugEnabled())
                {
                    log.Debug("Caching: {0}", key);
                }

                try
                {
                    cache.Lock(key);

                    ILockable lockable = (ILockable)cache.Get(key);

                    bool puttable = lockable == null ||
                                    lockable.IsPuttable(txTimestamp, version, versionComparator);

                    if (puttable)
                    {
                        cache.Put(key, CachedItem.Create(value, cache.NextTimestamp(), version));
                        if (log.IsDebugEnabled())
                        {
                            log.Debug("Cached: {0}", key);
                        }
                        return(true);
                    }
                    else
                    {
                        if (log.IsDebugEnabled())
                        {
                            if (lockable.IsLock)
                            {
                                log.Debug("Item was locked: {0}", key);
                            }
                            else
                            {
                                log.Debug("Item was already cached: {0}", key);
                            }
                        }
                        return(false);
                    }
                }
                finally
                {
                    cache.Unlock(key);
                }
            }
        }