aboutsummaryrefslogtreecommitdiff
path: root/lib/cache.ml
blob: 822c1fe911bee19217a2ddb63accec2660fda3aa (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module ChannelMap = Map.Make(Channel_id_t)
module GuildMap = Map.Make(Guild_id_t)
module UserMap = Map.Make(User_id_t)

type cache =
{ text_channels: Channel_t.guild_text ChannelMap.t
; voice_channels: Channel_t.guild_voice ChannelMap.t
; categories: Channel_t.category ChannelMap.t
; groups: Channel_t.group ChannelMap.t
; private_channels: Channel_t.dm ChannelMap.t
; guilds: Guild_t.t GuildMap.t
; presences: Presence.t UserMap.t
(* ; messages: Channel_id_t.t GuildMap.t *)
; unavailable_guilds: Guild_t.unavailable GuildMap.t
; user: User_t.t option
; users: User_t.t UserMap.t
}

type t =
{ lock: Lwt_mutex.t
; mutable cache: cache
}

let create () =
    let lock = Lwt_mutex.create () in
    let cache =
        { text_channels = ChannelMap.empty
        ; voice_channels = ChannelMap.empty
        ; categories = ChannelMap.empty
        ; groups = ChannelMap.empty
        ; private_channels = ChannelMap.empty
        ; guilds = GuildMap.empty
        ; presences = UserMap.empty
        ; unavailable_guilds = GuildMap.empty
        ; user = None
        ; users = UserMap.empty
        } in
    { lock; cache }

let cache = create ()

let update ({lock; cache} as t) f =
    Lwt_mutex.with_lock lock (fun () -> 
        let cache = f cache in
        t.cache <- cache;
        Lwt.return_unit)

let read_copy {lock; cache} =
    Lwt_mutex.with_lock lock (fun () -> Lwt.return cache)

let guild k cache = GuildMap.find_opt k cache.guilds

let text_channel k cache = ChannelMap.find_opt k cache.text_channels

let voice_channel k cache = ChannelMap.find_opt k cache.voice_channels

let category k cache = ChannelMap.find_opt k cache.categories

let dm k cache = ChannelMap.find_opt k cache.private_channels

let group k cache = ChannelMap.find_opt k cache.groups

let channel id cache : Channel_t.t option =
    let check = ChannelMap.find_opt in
    match check id cache.text_channels with
    | Some c -> Some (`GuildText c)
    | None -> (
    match check id cache.voice_channels with
    | Some c -> Some (`GuildVoice c)
    | None -> (
    match check id cache.categories with
    | Some c -> Some (`Category c)
    | None -> (
    match check id cache.private_channels with
    | Some c -> Some (`Private c)
    | None -> (
    match check id cache.groups with
    | Some c -> Some (`Group c)
    | None -> None
    ))))