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
72 lines (61 loc) · 2.16 KB
/
SessionFactory.cs
File metadata and controls
72 lines (61 loc) · 2.16 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
using System;
using System.Web;
using ServiceStack.CacheAccess;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
using ServiceStack.Text.Common;
namespace ServiceStack.ServiceInterface
{
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(IHttpRequest httpReq, IHttpResponse httpRes)
{
var sessionId = httpReq.GetSessionId()
?? httpRes.CreateSessionIds(httpReq);
return new SessionCacheClient(cacheClient, sessionId);
}
public ISession GetOrCreateSession()
{
if (HttpContext.Current != null)
return GetOrCreateSession(
HttpContext.Current.Request.ToRequest(),
HttpContext.Current.Response.ToResponse());
throw new NotImplementedException("Only ASP.NET Requests can be accessed via Singletons");
}
}
}