Exemplo n.º 1
0
        /// <summary>
        /// Stores an object to cache.
        /// </summary>
        /// <param name="typeId">The type identifier of the object.</param>
        /// <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{ICacheParameter}"/> 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>
        public static StorageEntry <ICacheParameter> Save(DataBuffer typeId, ICacheParameter instance, LocalCacheOptions options)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            if (!IsLocalCachingConfigured())
            {
                return(StorageEntry <ICacheParameter> .NotFound);
            }

            DateTime?  updated = null;
            StorageKey key;
            var        ext = instance as IExtendedCacheParameter;

            if (ext != null)
            {
                key     = new StorageKey(ext.ExtendedId, ext.PrimaryId);
                updated = ext.LastUpdatedDate;
            }
            else
            {
                var extRaw = instance as IExtendedRawCacheParameter;
                if (extRaw != null)
                {
                    key     = new StorageKey(extRaw.ExtendedId, extRaw.PrimaryId);
                    updated = extRaw.LastUpdatedDate;
                }
                else
                {
                    key = instance.PrimaryId;
                }
            }
            if (updated.HasValue)
            {
                if (options.Updated.HasValue && updated.Value !=
                    options.Updated.Value)
                {
                    ThrowUpdatedDateTimeNotAllowed();
                }
            }
            else
            {
                updated = options.Updated ?? DateTime.Now;
            }
            var entry = new StorageEntry <ICacheParameter>(instance, updated.Value,
                                                           GetRefreshExpires(typeId));

            _storage.Put(typeId, key, entry);
            SaveDependencies(typeId, key, updated.Value, options.ContentDependencies,
                             options.ExistenceDependencies);
            return(entry);
        }
        protected override ICacheParameter ReadOneParameter(JProperty item)
        {
            ICacheParameter param = base.ReadOneParameter(item);

            if (param is IAllowDefaultIfOptionWithoutValue paramWithNoValueDefault)
            {
                paramWithNoValueDefault.DefaultIfOptionWithoutValue = item.Value.ToString(nameof(IAllowDefaultIfOptionWithoutValue.DefaultIfOptionWithoutValue));
                return paramWithNoValueDefault as CacheParameter;
            }

            return param;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the key corresponding to an object.
        /// </summary>
        /// <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(ICacheParameter instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            var ext = instance as IExtendedCacheParameter;

            if (ext != null)
            {
                return(new StorageKey(ext.ExtendedId, ext.PrimaryId));
            }
            var extRaw = instance as IExtendedRawCacheParameter;

            if (extRaw != null)
            {
                return(new StorageKey(extRaw.ExtendedId, extRaw.PrimaryId));
            }
            return(instance.PrimaryId);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sets an object's key properties.
        /// </summary>
        /// <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(ICacheParameter instance, StorageKey key)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            instance.PrimaryId = key.PartitionId;
            var ext = instance as IExtendedCacheParameter;

            if (ext != null)
            {
                ext.ExtendedId = key.Key.ToString();
                return;
            }
            var extRaw = instance as IExtendedRawCacheParameter;

            if (extRaw != null)
            {
                extRaw.ExtendedId = key.Key.GetBinary();
            }
        }