Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace FSharpTools
- open System.Web
- open System.Web.Caching
- open System.Collections;
- open Microsoft.FSharp.Reflection
- //Modules are equivalent, machine code wise, to a static class in C#
- //So any methods defined in here should be callable as
- //TheOneCache.methodname
- //as long as the source file is using FsharpTools namespace
- module TheOneCache =
- type CacheType = Web | Redis
- //The ICache interface, which can be implemented for IIS cache, Redis, etc
- type ICache =
- abstract member Add: string -> 'T -> unit
- abstract member AddForTime: string -> 'T -> System.TimeSpan -> unit
- abstract member Get: string -> 'T option
- abstract member Exists: string -> bool
- abstract member Remove: string -> unit
- abstract member FlushAll: unit
- abstract member FlushKeysThatStartWith: string -> unit
- abstract member GetCountStartsWith: string -> int
- abstract member GetCacheType: CacheType
- //Implement the ICache interface as an IIS cache using an object expression
- let WebCache =
- { new ICache with
- member this.Add key value =
- HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null)
- member this.AddForTime key value duration =
- HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, duration, CacheItemPriority.NotRemovable, null)
- member this.Get<'T> key =
- match HttpRuntime.Cache.Get(key) with
- | null -> None
- | result -> Some (result :?> 'T)
- member this.Exists key =
- match HttpRuntime.Cache.Get(key) with
- | null -> false
- | result -> true
- member this.Remove key =
- HttpRuntime.Cache.Remove(key) |> ignore
- //IIS cache object uses a weird enumerator that is not F# friendly
- //So we have some imperative code
- member this.FlushAll =
- let enumerator = HttpRuntime.Cache.GetEnumerator()
- while enumerator.MoveNext() do
- HttpRuntime.Cache.Remove(enumerator.Key.ToString()) |> ignore
- member this.FlushKeysThatStartWith keyPrefix =
- let enumerator = HttpRuntime.Cache.GetEnumerator()
- while enumerator.MoveNext() do
- match enumerator.Key.ToString().StartsWith(keyPrefix) with
- | true -> HttpRuntime.Cache.Remove(enumerator.Key.ToString()) |> ignore
- | false -> ()
- //Oh no...mutable!
- member this.GetCountStartsWith keyPrefix =
- let enumerator = HttpRuntime.Cache.GetEnumerator()
- let mutable counter = 0
- while enumerator.MoveNext() do
- match enumerator.Key.ToString().StartsWith(keyPrefix) with
- | true -> counter <- counter + 1
- | false -> ()
- counter
- member this.GetCacheType =
- CacheType.Web
- }
- //Our list of caches, for now just the IIS cache
- let caches = [WebCache]
- //prints a union name as a string
- let unionToString (x:'a) =
- match FSharpValue.GetUnionFields(x, typeof<'a>) with
- | case, _ -> case.Name
- //Like an enum
- type EntryType = Marklogic | SQL | Netsuite | Other with
- member this.ToString = unionToString this
- //No need for objects, just functions
- let Add (entryType:EntryType, key, value) =
- caches |> List.iter (fun c -> c.Add (entryType.ToString + key) value )
- let AddForTime(entryType:EntryType, key, value, duration) =
- caches |> List.iter (fun c-> c.AddForTime (entryType.ToString + key) value duration)
- let Get<'T> (entryType:EntryType, key) =
- caches |> List.tryPick (fun c -> c.Get (entryType.ToString + key))
- let Exists (entryType:EntryType, key) =
- caches |> List.exists(fun c -> c.Exists (entryType.ToString + key) = true)
- let Remove (entryType:EntryType, key) =
- caches |> List.iter (fun c -> c.Remove (entryType.ToString + key ))
- let FlushAll =
- caches |> List.iter (fun c -> c.FlushAll)
- let FlushAllByType cacheType =
- caches |> List.filter (fun c -> c.GetCacheType = cacheType ) |> List.iter (fun c -> c.FlushAll)
- let FlushKeysThatStartWith keyPrefix cacheType =
- caches |> List.filter (fun c -> c.GetCacheType = cacheType ) |> List.iter (fun c -> c.FlushKeysThatStartWith keyPrefix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement