/// <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))); }
/// <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))); }
/// <summary> /// Gets the time to live of a cached object. /// </summary> /// <typeparam name="T">The type of the object.</typeparam> /// <param name="key">The key of the object.</param> /// <param name="options">Supplies virtual cache type, if any.</param> /// <returns>A <see cref="Nullable{DateTime}"/> containing the expiration /// time if found; otherwise <see langword="null"/>.</returns> /// <exception cref="ArgumentException"> /// <para>Type of <see cref="StorageKey.Key"/> of <paramref name="key"/> /// is wrong for the cache type of <typeparamref name="T"/>. /// </para> /// </exception> /// <exception cref="ApplicationException"> /// <typeparamref name="T"/> is a virtual cache type and <paramref name="options"/> /// has a null <see cref="LocalCacheOptions.VirtualCacheObject"/>. /// </exception> public static DateTime?GetExpires <T>(StorageKey key, LocalCacheOptions options) where T : ICacheParameter { CacheTypeStatic <T> .AssertProperStorageKey(key); return(GetExpires( GetKeySpace <T>(options.VirtualCacheObject), key)); }
/// <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))); }
/// <summary> /// Generates a reference to an object by identifier. /// </summary> /// <typeparam name="T">The type of the object. Must not /// implement <see cref="IExtendedCacheParameter"/> or /// <see cref="IExtendedRawCacheParameter"/>.</typeparam> /// <param name="objectId">The <see cref="StorageKey"/> /// of the object.</param> /// <param name="options">Supplies virtual cache type, if any.</param> /// <returns>The <see cref="ObjectReference"/> that specifies /// the object.</returns> /// <exception cref="ArgumentException"> /// <para>Type of <see cref="StorageKey.Key"/> of <paramref name="objectId"/> /// is wrong for the cache type of <typeparamref name="T"/>. /// </para> /// </exception> /// <exception cref="ApplicationException"> /// <typeparamref name="T"/> is a virtual cache type and <paramref name="options"/> /// has a null <see cref="LocalCacheOptions.VirtualCacheObject"/>. /// </exception> public static ObjectReference CreateReference <T>(StorageKey objectId, LocalCacheOptions options) where T : ICacheParameter { CacheTypeStatic <T> .AssertProperStorageKey(objectId); return(new ObjectReference(GetKeySpace <T>(options.VirtualCacheObject), objectId)); }
/// <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); }
/// <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); }
/// <summary> /// Retrieves an object from cache. /// </summary> /// <typeparam name="T">The type of the object.</typeparam> /// <param name="key">The key of the object.</param> /// <param name="options">The <see cref="LocalCacheOptions"/> to use for this /// operation.</param> /// <returns><see langword="true"/> if the object was found, otherwise /// <see langword="false"/>.</returns> /// <exception cref="ArgumentException"> /// <para>Type of <see cref="StorageKey.Key"/> of <paramref name="key"/> /// is wrong for the cache type of <typeparamref name="T"/>. /// </para> /// </exception> /// <exception cref="ApplicationException"> /// <typeparamref name="T"/> is a virtual cache type and <paramref name="options"/> /// has a null <see cref="LocalCacheOptions.VirtualCacheObject"/>. /// </exception> public static StorageEntry <T> Get <T>(StorageKey key, LocalCacheOptions options) where T : ICacheParameter { if (!IsLocalCachingConfigured()) { return(StorageEntry <T> .NotFound); } CacheTypeStatic <T> .AssertProperStorageKey(key); var entry = _storage.Get(GetKeySpace <T>(options.VirtualCacheObject), key, TypeStatic <T> .Creator); if (entry.IsFound) { entry.Instance.DataSource = DataSource.Cache; } return(entry); }
/// <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)); }
/// <summary> /// Deletes a cached object. /// </summary> /// <typeparam name="T">The type of the object.</typeparam> /// <param name="key">The key of the object.</param> /// <param name="options">Supplies virtual cache type, if any.</param> /// <returns><see langword="true"/> if the object existed prior, otherwise /// <see langword="false"/>.</returns> /// <exception cref="ArgumentException"> /// <para>Type of <see cref="StorageKey.Key"/> of <paramref name="key"/> /// is wrong for the cache type of <typeparamref name="T"/>. /// </para> /// </exception> /// <exception cref="ApplicationException"> /// <typeparamref name="T"/> is a virtual cache type and <paramref name="options"/> /// has a null <see cref="LocalCacheOptions.VirtualCacheObject"/>. /// </exception> public static bool Delete <T>(StorageKey key, LocalCacheOptions options) where T : ICacheParameter { CacheTypeStatic <T> .AssertProperStorageKey(key); return(Delete(GetKeySpace <T>(options.VirtualCacheObject), key)); }