forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionFactory.cs
More file actions
67 lines (57 loc) · 1.83 KB
/
Copy pathSessionFactory.cs
File metadata and controls
67 lines (57 loc) · 1.83 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
using System;
using System.Web;
using ServiceStack.Caching;
using ServiceStack.Text.Common;
using ServiceStack.Web;
namespace ServiceStack
{
public class SessionFactory : ISessionFactory
{
private readonly ICacheClient cacheClient;
public SessionFactory(ICacheClient cacheClient)
{
this.cacheClient = cacheClient;
}
public class SessionCacheClient : ISession
{
private readonly ICacheClient cacheClient;
private string prefixNs;
public SessionCacheClient(ICacheClient cacheClient, string sessionId)
{
this.cacheClient = cacheClient;
this.prefixNs = "sess:" + sessionId + ":";
}
public object this[string key]
{
get
{
return cacheClient.Get<object>(this.prefixNs + key);
}
set
{
JsWriter.WriteDynamic(() =>
cacheClient.Set(this.prefixNs + key, value));
}
}
public void Set<T>(string key, T value)
{
cacheClient.Set(this.prefixNs + key, value);
}
public T Get<T>(string key)
{
return cacheClient.Get<T>(this.prefixNs + key);
}
}
public ISession GetOrCreateSession(IRequest httpReq, IResponse httpRes)
{
var sessionId = httpReq.GetSessionId()
?? httpRes.CreateSessionIds(httpReq);
return new SessionCacheClient(cacheClient, sessionId);
}
public ISession GetOrCreateSession()
{
var request = HostContext.GetCurrentRequest();
return GetOrCreateSession(request, request.Response);
}
}
}