Skip to content

Commit

Permalink
method to check etag
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasamiller committed Sep 7, 2023
1 parent af94c91 commit 69f3f67
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 6 additions & 2 deletions AzureBlobCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ open System.Text.Json
| null -> None
| v -> Some(v :?> CacheEntry)


member this.matchesEtag(etag: ETag, blobName: string) =
match getFromMemoryCache blobName with
| Some(v) -> v.ETag = etag
| None -> false

member private this.RefreshCacheIfStale(blobName : string) =
async {
Expand Down Expand Up @@ -71,7 +74,8 @@ open System.Text.Json
}


member this.GetJsonBlob(blobName : string) =

member this.GetBlob(blobName : string) =
task {
return!
async {
Expand Down
16 changes: 8 additions & 8 deletions AzureUtils.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ module Tests =
blobClient.DeleteIfExists() |> ignore
blobClient.Upload(new BinaryData(testCat1)) |> ignore
let underTest = new AzureUtils.AzureBlobCache(connectionString,containerName,TimeSpan.FromSeconds(10), memoryCache, loggerFactory)
let r1 = underTest.GetJsonBlob(testBlobName).Result.ToObjectFromJson<Cat>()
let r1 = underTest.GetBlob(testBlobName).Result.ToObjectFromJson<Cat>()
blobClient.DeleteIfExists() |> ignore
let r2 = underTest.GetJsonBlob(testBlobName).Result.ToObjectFromJson<Cat>()
let r2 = underTest.GetBlob(testBlobName).Result.ToObjectFromJson<Cat>()
// should not have updated, cache should be stale
Assert.Equal(r1,r2)

Expand All @@ -77,11 +77,11 @@ module Tests =
blobClient.DeleteIfExists() |> ignore
blobClient.Upload(new BinaryData(testCat1)) |> ignore
let underTest = new AzureUtils.AzureBlobCache(connectionString,containerName, TimeSpan.FromMilliseconds(10), memoryCache, loggerFactory)
let r1 = underTest.GetJsonBlob(testBlobName).Result.ToObjectFromJson<Cat>()
let r1 = underTest.GetBlob(testBlobName).Result.ToObjectFromJson<Cat>()
blobClient.DeleteIfExists() |> ignore
blobClient.Upload(new BinaryData(testCat2)) |> ignore
Thread.Sleep(11)
let r2 = underTest.GetJsonBlob(testBlobName).Result.ToObjectFromJson<Cat>()
let r2 = underTest.GetBlob(testBlobName).Result.ToObjectFromJson<Cat>()
// should have updated
Assert.NotEqual(r1,r2)

Expand All @@ -92,7 +92,7 @@ module Tests =
blobClient.DeleteIfExists() |> ignore
let underTest = new AzureUtils.AzureBlobCache(connectionString,containerName, TimeSpan.FromHours(1), memoryCache, loggerFactory)
blobClient.Upload(new BinaryData(testCat1)) |> ignore
let r = underTest.GetJsonBlob(testBlobName).Result
let r = underTest.GetBlob(testBlobName).Result
let expectedData = new BinaryData(testCat1)
let cat = expectedData.ToObjectFromJson<Cat>()
Assert.Equal(cat,testCat1)
Expand All @@ -101,7 +101,7 @@ module Tests =
let ``getting blob that does not exist should return null`` () =

let underTest = new AzureUtils.AzureBlobCache(connectionString,containerName, TimeSpan.FromHours(1), memoryCache, loggerFactory)
let result = underTest.GetJsonBlob("doesNotExist.json").Result
let result = underTest.GetBlob("doesNotExist.json").Result
Assert.Null(result)

[<Fact>]
Expand All @@ -110,8 +110,8 @@ module Tests =
let underTest = new AzureUtils.AzureBlobCache(connectionString,containerName, TimeSpan.FromHours(1), memoryCache, loggerFactory)
underTest.WriteBlobAsync("cat1",testCat1).Result |> ignore
underTest.WriteBlobAsync("cat2",testCat2).Result |> ignore
let c1 = underTest.GetJsonBlob("cat1").Result.ToObjectFromJson<Cat>()
let c2 = underTest.GetJsonBlob("cat2").Result.ToObjectFromJson<Cat>()
let c1 = underTest.GetBlob("cat1").Result.ToObjectFromJson<Cat>()
let c2 = underTest.GetBlob("cat2").Result.ToObjectFromJson<Cat>()
Assert.Equal(testCat1,c1)
Assert.Equal(testCat2,c2)

2 changes: 1 addition & 1 deletion AzureUtils.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageId>Nam.AzureUtils</PackageId>
<Version>0.13.0</Version>
<Version>0.15.0</Version>
<Authors>Nicholas Miller</Authors>
<Description>
Miscellaneous utils for working with Azure from .NET SDK.
Expand Down

0 comments on commit 69f3f67

Please sign in to comment.