Пример #1
0
        private static DateTime GetRefreshExpires <T>(T instance, IVirtualCacheType virtualCacheObject)
            where T : ICacheParameter
        {
            var ttl = TypeStatic <T> .GetDescription(instance, virtualCacheObject).Ttl;

            return(DateTime.Now.AddSeconds(ttl));
        }
Пример #2
0
        /// <summary>
        /// Generates a reference to an object.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="instance">The <typeparamref name="T"/>
        /// being referred to.</param>
        /// <returns>The <see cref="ObjectReference"/> that specifies
        /// <paramref name="instance"/>.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        public static ObjectReference CreateReference <T>(T instance) where T : ICacheParameter
        {
            TypeStatic <T> .AssertNotNull("instance", instance);

            return(new ObjectReference(GetKeySpace(instance, null),
                                       CacheTypeStatic <T> .GetKey(instance)));
        }
Пример #3
0
        /// <summary>
        /// Deletes a cached object.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="instance">Required. The object to refresh.</param>
        /// <returns><see langword="true"/> if the object existed prior, otherwise
        /// <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        public static bool Delete <T>(T instance) where T : ICacheParameter
        {
            TypeStatic <T> .AssertNotNull("instance", instance);

            return(Delete(
                       GetKeySpace(instance, null),
                       CacheTypeStatic <T> .GetKey(instance)));
        }
Пример #4
0
        /// <summary>
        /// Gets the expiration time of a cached object.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="instance">Required. The object to refresh.</param>
        /// <returns>A <see cref="Nullable{DateTime}"/> containing the expiration
        /// time if found; otherwise <see langword="null"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        public static DateTime?GetExpires <T>(T instance) where T : ICacheParameter
        {
            TypeStatic <T> .AssertNotNull("instance", instance);

            return(GetExpires(
                       GetKeySpace(instance, null),
                       CacheTypeStatic <T> .GetKey(instance)));
        }
Пример #5
0
        private static DataBuffer GetKeySpace <T>()
            where T : ICacheParameter
        {
            if (TypeStatic <T> .IsVirtual)
            {
                throw new ApplicationException(string.Format(
                                                   "Cannot be called for virtual cache type {0} without a virtual cache instance, please use an overload that supplies an instance",
                                                   TypeStatic <T> .TypeName));
            }
            TypeStatic <T> .AssertDescriptionFound(TypeStatic <T> .Description,
                                                   TypeStatic <T> .TypeName);

            return(TypeStatic <T> .Description.KeySpace);
        }
Пример #6
0
        GetDependencies(DataBuffer typeId, StorageKey key, bool create)
        {
            var listKey = GetDependencyKey(typeId, key);

            if (create)
            {
                var ttl = TypeStatic <ObjectDependency> .GetDescription(null, null).Ttl;

                var expires = DateTime.Now.AddSeconds(ttl);
                return(_storage.GetOrCreateList(ListTypeId, listKey,
                                                new ObjectReference(typeId, key), expires, CreateBlankDependency,
                                                CreateBlankReference));
            }
            return(_storage.GetList(ListTypeId, listKey, CreateBlankDependency,
                                    CreateBlankReference));
        }
Пример #7
0
        /// <summary>
        /// Sets an object's key properties.
        /// </summary>
        /// <typeparam name="T">The type of <paramref name="instance"/>.</typeparam>
        /// <param name="instance">The object.</param>
        /// <param name="key">The <see cref="StorageKey"/> identifier key.</param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        public static void SetKey <T>(T instance, StorageKey key) where T : ICacheParameter
        {
            TypeStatic <T> .AssertNotNull("instance", instance);

            instance.PrimaryId = key.PartitionId;
            if (CacheTypeStatic <T> .IsExtendedCache)
            {
                ((IExtendedCacheParameter)instance).ExtendedId =
                    key.Key.ToString();
            }
            else if (CacheTypeStatic <T> .IsExtendedRawCache)
            {
                ((IExtendedRawCacheParameter)instance).ExtendedId =
                    key.Key.GetBinary();
            }
        }
Пример #8
0
        /// <summary>
        /// Stores an object to cache.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="instance">Required. The object to store.</param>
        /// <param name="options">The <see cref="LocalCacheOptions"/> to use for this
        /// operation.</param>
        /// <returns>The <see cref="StorageEntry{T}"/> containing the data
        /// saved.  <see cref="StorageEntry{T}.NotFound"/> if local caching is disabled.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <para><see cref="LocalCacheOptions.Updated"/> of <paramref name="options"/>
        /// has a value specified, which is not allowed for the cache type of
        /// <typeparamref name="T"/>.</para>
        /// </exception>
        public static StorageEntry <T> Save <T>(T instance, LocalCacheOptions options)
            where T : ICacheParameter
        {
            if (!IsLocalCachingConfigured())
            {
                return(StorageEntry <T> .NotFound);
            }

            TypeStatic <T> .AssertNotNull("instance", instance);

            DateTime?updated = null;

            if (CacheTypeStatic <T> .IsExtendedCache)
            {
                var ext = (IExtendedCacheParameter)instance;
                updated = ext.LastUpdatedDate;
            }
            else if (CacheTypeStatic <T> .IsExtendedRawCache)
            {
                var extRaw = (IExtendedRawCacheParameter)instance;
                updated = extRaw.LastUpdatedDate;
            }
            if (updated.HasValue)
            {
                if (options.Updated.HasValue && updated.Value !=
                    options.Updated.Value)
                {
                    ThrowUpdatedDateTimeNotAllowed();
                }
            }
            else
            {
                updated = options.Updated ?? DateTime.Now;
            }
            var key = CacheTypeStatic <T> .GetKey(instance);

            var entry = new StorageEntry <T>(instance, updated.Value,
                                             GetRefreshExpires(instance, null));
            var typeId = GetKeySpace(instance, null);

            _storage.Put(typeId, key, entry);
            SaveDependencies(typeId, key, updated.Value, options.ContentDependencies,
                             options.ExistenceDependencies);
            return(entry);
        }
Пример #9
0
        /// <summary>
        /// Retrieves an object from cache.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="instance">Required. Instance to supply key, and virtual cache
        /// type if any.</param>
        /// <returns><see langword="true"/> if the object was found, otherwise
        /// <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        public static StorageEntry <T> Get <T>(T instance)
            where T : ICacheParameter
        {
            if (!IsLocalCachingConfigured())
            {
                return(StorageEntry <T> .NotFound);
            }

            TypeStatic <T> .AssertNotNull("instance", instance);

            var entry = _storage.Get(GetKeySpace(instance, null), GetKey(instance),
                                     instance);

            if (entry.IsFound)
            {
                entry.Instance.DataSource = DataSource.Cache;
            }
            return(entry);
        }
Пример #10
0
 private static DataBuffer GetKeySpace <T>(T instance, IVirtualCacheType virtualCacheObject)
     where T : ICacheParameter
 {
     return(TypeStatic <T> .GetDescription(instance, virtualCacheObject).KeySpace);
 }
Пример #11
0
        /// <summary>
        /// Gets the key corresponding to an object.
        /// </summary>
        /// <typeparam name="T">The type of <paramref name="instance"/>.</typeparam>
        /// <param name="instance">The object.</param>
        /// <returns>The <see cref="StorageKey"/> identifier key.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="instance"/> is <see langword="null"/>.</para>
        /// </exception>
        public static StorageKey GetKey <T>(T instance) where T : ICacheParameter
        {
            TypeStatic <T> .AssertNotNull("instance", instance);

            return(CacheTypeStatic <T> .GetKey(instance));
        }