aboutsummaryrefslogtreecommitdiff
path: root/libs/VNLib.Plugins.Sessions.Cache.Client/src/RemoteSession.cs
blob: 421d07dbf544eff31f2263b9bab725aa71271fcc (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Sessions.Cache.Client
* File: RemoteSession.cs 
*
* RemoteSession.cs is part of VNLib.Plugins.Sessions.Cache.Client which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Sessions.Cache.Client is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Sessions.Cache.Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Collections.Generic;

using VNLib.Utils.Extensions;
using VNLib.Plugins.Essentials.Sessions;
using VNLib.Plugins.Essentials.Extensions;


namespace VNLib.Plugins.Sessions.Cache.Client
{
    /// <summary>
    /// Base class for cacheable lazy initialized session entires 
    /// that exist in a remote caching server
    /// </summary>
    public abstract class RemoteSession : SessionBase, IRemoteSession
    {
        protected const string CREATED_TIME_ENTRY = "__.i.ctime";

        /// <summary>
        /// The session data store
        /// </summary>
        protected readonly IDictionary<string, string> DataStore;

        /// <summary>
        /// The reason that the session was destroyed if an error occured
        /// </summary>
        protected Exception? ErrorCause { get; set; }

        /// <summary>
        /// Initialzies a new session
        /// </summary>
        /// <param name="sessionId">The id of the current session</param>
        /// <param name="initialData">The initial data</param>
        /// <param name="isNew">A flag that determines if the session is considered new</param>
        protected RemoteSession(string sessionId, IDictionary<string, string> initialData, bool isNew)
        {
            SessionID = sessionId;
            DataStore = initialData;
            IsNew = isNew;
        }

        ///<inheritdoc/>
        public override string SessionID { get; }

        ///<inheritdoc/>
        public override DateTimeOffset Created
        {
            get
            {
                //Deserialze the base32 ms
                long unixMs = this.GetValueType<string, long>(CREATED_TIME_ENTRY);
                //set created time from ms
                return DateTimeOffset.FromUnixTimeMilliseconds(unixMs);
            }

            set => this.SetValueType(CREATED_TIME_ENTRY, value.ToUnixTimeMilliseconds());
        }     

        ///<inheritdoc/>
        protected override string IndexerGet(string key)
        {
            //Get the value at the key or an empty string as a default
            return DataStore.GetValueOrDefault(key) ?? string.Empty;
        }
        ///<inheritdoc/>
        protected override void IndexerSet(string key, string value)
        {
            //If the value is null, remove the key from the store
            if (value == null)
            {
                //Set modified flag 
                IsModified |= DataStore.Remove(key);
            }
            else
            {
                //Store the value at the specified key
                DataStore[key] = value;
                IsModified = true;
            }
        }
    

        ///<inheritdoc/>
        public virtual SessionStatus GetStatus()
        {
            SessionStatus status = SessionStatus.None;

            status |= Flags.IsSet(INVALID_MSK) ? SessionStatus.Delete : SessionStatus.None;
            status |= Flags.IsSet(REGEN_ID_MSK) ? SessionStatus.RegenId : SessionStatus.None;
            status |= Flags.IsSet(MODIFIED_MSK) ? SessionStatus.UpdateOnly: SessionStatus.None;
            
            return status;
        }

        ///<inheritdoc/>
        public virtual IDictionary<string, string> GetSessionData() => DataStore;

        ///<inheritdoc/>
        public virtual void Destroy(Exception? cause)
        {
            //Set invalid status
            ErrorCause = cause;
            Flags.Set(INVALID_MSK);
            
            //Clear all session data
            DataStore.Clear();
        }

        ///<inheritdoc/>
        public virtual bool IsValid(out Exception? cause)
        {
            /*
             * Were reusing the invalid mask assuming that when a session is invalidated
             * it will be deleted and destroyed
             */

            cause = ErrorCause;
            return !Flags.IsSet(INVALID_MSK);
        }

        ///<inheritdoc/>
        public virtual void SessionUpdateComplete()
        {
            //Reset flags
            Flags.ClearAll();
        }
    }
}