Exemplo n.º 1
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)));
        }
Exemplo n.º 2
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)));
        }
Exemplo n.º 3
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)));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generates a array of references to an array of objects.
        /// </summary>
        /// <typeparam name="T">The type of the objects.</typeparam>
        /// <param name="instances">The <typeparamref name="T"/> array
        /// being referred to.</param>
        /// <returns>The array of <see cref="ObjectReference"/>s that
        /// contains references specified by <paramref name="instances"/>.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="instances"/> is <see langword="null"/>.</para>
        /// </exception>
        public static ObjectReference[] CreateReferences <T>(params T[] instances) where T : ICacheParameter
        {
            if (instances == null)
            {
                throw new ArgumentNullException("instances");
            }
            var ret = new ObjectReference[instances.Length];
            var idx = 0;

            foreach (var instance in instances)
            {
                ret[idx] = new ObjectReference(GetKeySpace(instance, null),
                                               CacheTypeStatic <T> .GetKey(instance));
                ++idx;
            }
            return(ret);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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));
        }