Use image last updated time in cache key #149
-
We have a bunch of servers, all of which use the same filesystem for images.
Because the cache key is based only on the URL, the write is happening when another server is reading, causing the exception. Assuming that the metadata load has to happen there, and not earlier (IDK if that's the case), I think the only option here is to allow user to set a method This would mean that there are (if the user decides to implement That new URI returned by the Alternatively, instead of keeping track of a source key and modifying it when we need a cache key, we could /// <summary>
/// Contains the key used to identify the source image and cached image.
/// </summary>
public class ImageKeyCollection
{
protected uint Length { get; }
/// <summary>
/// The data that the (hashed) key is based on.
/// </summary>
protected string KeySource { get; }
protected ICacheHash CacheHash { get; }
/// <summary>
/// A key which identifies the source image.
/// </summary>
public string ImageSourceKey { get; }
/// <summary>
/// A key which identifies the cached image.
/// By default, same as source key.
/// It is an error to call this method before <see cref="SourceImageMetadata"/>
/// </summary>
public virtual string ImageCacheKey => this.ImageSourceKey;
public ImageMetadata? SourceImageMetadata { get; set; }
public ImageKeyCollection(string keySource, ICacheHash cacheHash, uint length)
{
this.KeySource = keySource;
this.ImageSourceKey = this.CacheHash.Create(this.KeySource, length);
this.CacheHash = cacheHash;
this.Length = length;
}
}
public class VaryByModifiedDateImageKeyCollection : ImageKeyCollection
{
private string _imageCacheKey;
public VaryByModifiedDateImageKeyCollection(string uri, ICacheHash cacheHash, uint length) : base(uri, cacheHash, length)
{
}
public override string ImageCacheKey
{
get
{
if (this._imageCacheKey != null)
{
return this._imageCacheKey;
}
if (!this.SourceImageMetadata.HasValue)
{
ThrowHelper.ThrowArgumentNullExceptionForNotNull(nameof(this.SourceImageMetadata));
}
this._imageCacheKey = this.CacheHash.Create($"{this.KeySource}{this.SourceImageMetadata.Value.LastWriteTimeUtc.Ticks}", this.Length);
return this._imageCacheKey;
}
}
} @JimBobSquarePants, what do you say? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm afraid I don't believe this is a viable solution. You shouldn't be sharing a filesystem across multiple independent servers in that manner. You should be using a cache that handles concurrency like Azure Blob Storage |
Beta Was this translation helpful? Give feedback.
I'm afraid I don't believe this is a viable solution. You shouldn't be sharing a filesystem across multiple independent servers in that manner. You should be using a cache that handles concurrency like Azure Blob Storage