-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache folder configuration for Azure and AWS Image Caches (Lombiq Technologies: OCORE-136) #353
Changes from 4 commits
98a43eb
5ff144c
40b5b8c
9db0e6b
408610b
dbcae3a
e41bc2a
ff7fdc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,11 @@ public class AWSS3StorageCacheOptions : IAWSS3BucketClientOptions | |
/// <inheritdoc/> | ||
public string BucketName { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets or sets the cache folder's name that'll store cache files under the configured bucket. | ||
/// </summary> | ||
public string CacheFolder { get; set; } = null!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want this to be nullable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually do. I changed this for the Azure class too. |
||
|
||
/// <inheritdoc/> | ||
public string? AccessKey { get; set; } | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ namespace SixLabors.ImageSharp.Web.Caching.Azure; | |
public class AzureBlobStorageCache : IImageCache | ||
{ | ||
private readonly BlobContainerClient container; | ||
private readonly string cacheFolder; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzureBlobStorageCache"/> class. | ||
|
@@ -27,12 +28,15 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions | |
AzureBlobStorageCacheOptions options = cacheOptions.Value; | ||
|
||
this.container = new BlobContainerClient(options.ConnectionString, options.ContainerName); | ||
this.cacheFolder = string.IsNullOrEmpty(options.CacheFolder) | ||
? string.Empty | ||
: options.CacheFolder.Trim().Trim('/') + '/'; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<IImageCacheResolver?> GetAsync(string key) | ||
{ | ||
BlobClient blob = this.container.GetBlobClient(key); | ||
BlobClient blob = this.container.GetBlobClient(this.GetBlobName(key)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should instead perhaps the whole right side be a method, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored this. |
||
|
||
if (!await blob.ExistsAsync()) | ||
{ | ||
|
@@ -45,7 +49,7 @@ public AzureBlobStorageCache(IOptions<AzureBlobStorageCacheOptions> cacheOptions | |
/// <inheritdoc/> | ||
public Task SetAsync(string key, Stream stream, ImageCacheMetadata metadata) | ||
{ | ||
BlobClient blob = this.container.GetBlobClient(key); | ||
BlobClient blob = this.container.GetBlobClient(this.GetBlobName(key)); | ||
|
||
BlobHttpHeaders headers = new() | ||
{ | ||
|
@@ -79,4 +83,7 @@ public static Response<BlobContainerInfo> CreateIfNotExists( | |
AzureBlobStorageCacheOptions options, | ||
PublicAccessType accessType) | ||
=> new BlobContainerClient(options.ConnectionString, options.ContainerName).CreateIfNotExists(accessType); | ||
|
||
private string GetBlobName(string key) | ||
=> this.cacheFolder + key; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,15 @@ public class AzureBlobStorageCacheOptions | |
|
||
/// <summary> | ||
/// Gets or sets the Azure Blob Storage container name. | ||
/// Must conform to Azure Blob Storage container naming guidlines. | ||
/// Must conform to Azure Blob Storage container naming guidelines. | ||
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names"/> | ||
/// </summary> | ||
public string ContainerName { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets or sets the cache folder's name that'll store cache files under the configured container. | ||
/// Must conform to Azure Blob Storage directory naming guidelines. | ||
/// <see href="https://learn.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#directory-names"/> | ||
/// </summary> | ||
public string CacheFolder { get; set; } = null!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want this to be nullable? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I DRY this into a helper class, not to copy it between the two cache implementations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave as is and keep them separate. They're two separate assemblies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK then.