Skip to content

Commit

Permalink
fixed caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium authored and sergey-tihon committed May 30, 2020
1 parent fd99d00 commit 41576a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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.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

0 comments on commit 41576a2

Please sign in to comment.