Skip to content
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

Fixed caching #153

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SwaggerProvider.DesignTime/Caching.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type ICache<'TKey, 'TValue> =
abstract Set : key:'TKey * value:'TValue -> unit
abstract TryRetrieve : key:'TKey * ?extendCacheExpiration:bool -> 'TValue option
abstract Remove : key:'TKey -> unit
abstract GetOrAdd : key:'TKey * valueFactory:(unit -> 'TValue) -> 'TValue

/// Creates a cache that uses in-memory collection
let createInMemoryCache (expiration:TimeSpan) =
Expand Down Expand Up @@ -106,4 +107,8 @@ let createInMemoryCache (expiration:TimeSpan) =
match dict.TryRemove(key) with
| true, _ -> log (sprintf "Explicitly removed from cache: %O" key)
| _ -> ()
}
member __.GetOrAdd(key, valueFactory) =
let res, _ = dict.GetOrAdd(key, fun k -> valueFactory(), DateTime.UtcNow)
invalidationFunction key |> Async.Start
res
}
6 changes: 2 additions & 4 deletions src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ type public OpenApiClientTypeProvider(cfg : TypeProviderConfig) as this =
|> sprintf "%A"


match Cache.providedTypes.TryRetrieve(cacheKey) with
| Some(ty) -> ty
| None ->
let addCache() =
let schemaData =
match schemaPathRaw.StartsWith("http", true, null) with
| true ->
Expand Down Expand Up @@ -88,8 +86,8 @@ type public OpenApiClientTypeProvider(cfg : TypeProviderConfig) as this =
ty.AddMembers tys
tempAsm.AddTypes [ty]

Cache.providedTypes.Set(cacheKey, ty)
ty
Cache.providedTypes.GetOrAdd(cacheKey, addCache)
)
t
do
Expand Down
6 changes: 2 additions & 4 deletions src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ type public SwaggerTypeProvider(cfg : TypeProviderConfig) as this =
(schemaPathRaw, headersStr, ignoreOperationId, ignoreControllerPrefix, preferNullable, preferAsync)
|> sprintf "%A"

match Cache.providedTypes.TryRetrieve(cacheKey) with
| Some(ty) -> ty
| None ->
let addCache() =
let schemaData =
match schemaPathRaw.StartsWith("http", true, null) with
| true ->
Expand Down Expand Up @@ -106,8 +104,8 @@ type public SwaggerTypeProvider(cfg : TypeProviderConfig) as this =
ty.AddMembers tys
tempAsm.AddTypes [ty]

Cache.providedTypes.Set(cacheKey, ty)
ty
Cache.providedTypes.GetOrAdd(cacheKey, addCache)
)
t
do
Expand Down