forked from fsprojects/Rezoom.SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserModelCache.fs
More file actions
40 lines (35 loc) · 1.36 KB
/
UserModelCache.fs
File metadata and controls
40 lines (35 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace Rezoom.SQL.Provider
open System
open System.Collections.Generic
open System.IO
open Rezoom.SQL.Compiler
type private UserModelCache() as this =
let watchers = Dictionary()
let cache = Dictionary()
let invalidated = Event<EventHandler, EventArgs>()
let addWatcher path invalidateKey =
let succ, watcher = watchers.TryGetValue(path)
let watcher =
if succ then watcher else
let watcher = new Watcher(path)
watcher.Invalidated.Add(fun _ -> invalidated.Trigger(this, EventArgs.Empty))
watchers.Add(path, watcher)
watcher
watcher.Invalidating.Add(fun _ -> ignore <| cache.Remove(invalidateKey)) // remove from cache on changes
[<CLIEvent>]
member __.Invalidated = invalidated.Publish
member this.Load(resolutionFolder, modelPath) =
let key = (resolutionFolder, modelPath)
let succ, cachedModel = cache.TryGetValue(key)
if succ then cachedModel else
let model = UserModel.Load(resolutionFolder, modelPath)
cache.[key] <- model
addWatcher model.ConfigDirectory key
addWatcher model.MigrationsDirectory key
model
member this.Dispose() =
for KeyValue(_, w) in watchers do
w.Dispose()
watchers.Clear()
interface IDisposable with
member this.Dispose() = this.Dispose()