-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSessionFactory.cs
More file actions
148 lines (124 loc) · 5.02 KB
/
Copy pathSessionFactory.cs
File metadata and controls
148 lines (124 loc) · 5.02 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
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
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Caching;
using ServiceStack.Text;
using ServiceStack.Text.Common;
using ServiceStack.Web;
namespace ServiceStack
{
public class SessionFactory : ISessionFactory
{
private readonly ICacheClient cacheClient;
private readonly ICacheClientAsync cacheClientAsync;
public SessionFactory(ICacheClient cacheClient)
: this(cacheClient, null) {}
public SessionFactory(ICacheClient cacheClient, ICacheClientAsync cacheClientAsync)
{
this.cacheClient = cacheClient;
this.cacheClientAsync = cacheClientAsync ?? cacheClient.AsAsync();
}
public class SessionCacheClient : ISession
{
private readonly ICacheClient cacheClient;
private readonly string prefixNs;
public SessionCacheClient(ICacheClient cacheClient, string sessionId)
{
this.cacheClient = cacheClient;
this.prefixNs = "sess:" + sessionId + ":";
}
private string EnsurePrefix(string s) => s != null && !s.StartsWith(prefixNs)
? prefixNs + s
: s;
public object this[string key]
{
get => cacheClient.Get<object>(EnsurePrefix(key));
set
{
JsWriter.WriteDynamic(() =>
cacheClient.Set(EnsurePrefix(key), value));
}
}
public void Set<T>(string key, T value)
{
var expiry = HostContext.GetPlugin<SessionFeature>()?.SessionBagExpiry;
if (expiry != null)
cacheClient.Set(EnsurePrefix(key), value, expiry.Value);
else
cacheClient.Set(EnsurePrefix(key), value);
}
public T Get<T>(string key)
{
return cacheClient.Get<T>(EnsurePrefix(key));
}
public bool Remove(string key)
{
return cacheClient.Remove(EnsurePrefix(key));
}
public void RemoveAll()
{
cacheClient.RemoveByPattern(this.prefixNs + "*");
}
}
public class SessionCacheClientAsync : ISessionAsync
{
private readonly ICacheClientAsync cacheClient;
private readonly string prefixNs;
public SessionCacheClientAsync(ICacheClientAsync cacheClient, string sessionId)
{
this.cacheClient = cacheClient;
this.prefixNs = "sess:" + sessionId + ":";
}
private string EnsurePrefix(string s) => s != null && !s.StartsWith(prefixNs)
? prefixNs + s
: s;
public async Task SetAsync<T>(string key, T value, CancellationToken token=default)
{
var expiry = HostContext.GetPlugin<SessionFeature>()?.SessionBagExpiry;
if (expiry != null)
await cacheClient.SetAsync(EnsurePrefix(key), value, expiry.Value, token).ConfigAwait();
else
await cacheClient.SetAsync(EnsurePrefix(key), value, token).ConfigAwait();
}
public Task<T> GetAsync<T>(string key, CancellationToken token=default)
{
return cacheClient.GetAsync<T>(EnsurePrefix(key), token);
}
public Task<bool> RemoveAsync(string key, CancellationToken token=default)
{
return cacheClient.RemoveAsync(EnsurePrefix(key), token);
}
public Task RemoveAllAsync(CancellationToken token=default)
{
return cacheClient.RemoveByPatternAsync(this.prefixNs + "*", token);
}
}
public ISession GetOrCreateSession(IRequest httpReq, IResponse httpRes)
{
var sessionId = httpReq.GetSessionId() ?? httpRes.CreateSessionIds(httpReq);
return new SessionCacheClient(cacheClient, sessionId);
}
public ISessionAsync GetOrCreateSessionAsync(IRequest httpReq, IResponse httpRes)
{
var sessionId = httpReq.GetSessionId() ?? httpRes.CreateSessionIds(httpReq);
return new SessionCacheClientAsync(cacheClientAsync, sessionId);
}
public ISession GetOrCreateSession()
{
var request = HostContext.GetCurrentRequest();
return GetOrCreateSession(request, request.Response);
}
public ISessionAsync GetOrCreateSessionAsync()
{
var request = HostContext.GetCurrentRequest();
return GetOrCreateSessionAsync(request, request.Response);
}
public ISession CreateSession(string sessionId)
{
return new SessionCacheClient(cacheClient, sessionId);
}
public ISessionAsync CreateSessionAsync(string sessionId)
{
return new SessionCacheClientAsync(cacheClientAsync, sessionId);
}
}
}