My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Xml.Serialization;
using System.Web.Caching;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;

namespace Rapid.Tools.Domain
{
public abstract class RapidCachedDataAdapterBase<TData> where TData : class
{
private static readonly object _lock = new object();
private TData _data;
private Guid _siteID;

protected virtual bool AutoRefreshCache {
get
{
return true;
}
}

protected virtual string CacheKey
{
get
{
return string.Format("RapidTools.{0}.Data", GetType().Name);
}
}

protected virtual string CacheExpireKey
{
get
{
return string.Format("{0}.Expire", CacheKey);
}
}

protected virtual int CacheTimeoutMinutes {
get
{
return 20;
}
}

protected Guid SiteID
{
get
{
return _siteID;
}
}

public virtual TData CachedData
{
get
{
if (_data == null)
_data = GetData();

return _data;
}
}


public RapidCachedDataAdapterBase(Guid siteID)
{
_siteID = siteID;
}

public RapidCachedDataAdapterBase(SPSite site)
{
_siteID = site.ID;
}


protected abstract TData GetNativeData();

protected virtual TData GetData()
{
TData ret = GetCache();

if (ret == null)
{
ret = GetNativeData();
SetCache(ret);
}

return ret;
}

protected virtual TData GetCache()
{
TData ret = null;
string sdata;

var ctx = HttpContext.Current;

if (ctx != null)
{
var ctxitem = ctx.Items[CacheKey];
if (ctxitem != null && ctxitem is TData)
{
ret = (TData)ctxitem;
}
}

if (ret == null)
{
lock (_lock)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (SPSite esite = new SPSite(SiteID))
{
var expired = true;
object oexpire = esite.RootWeb.AllProperties[CacheExpireKey];

if (oexpire != null && oexpire is DateTime)
{
var dexpire = (DateTime)oexpire;
expired = (DateTime.Now >= dexpire);
}

if (!expired)
{
sdata = (string)esite.RootWeb.AllProperties[CacheKey];
if (!string.IsNullOrEmpty(sdata))
{
ret = Deserialize(sdata);
}
}
}
});
}
}

if (ctx != null)
ctx.Items[CacheKey] = ret;

return ret;
}


protected virtual void SetCache(TData data)
{
string sdata;

lock (_lock)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (SPSite esite = new SPSite(SiteID))
{
sdata = Serialize(data);
esite.RootWeb.AllProperties[CacheKey] = sdata;
esite.RootWeb.AllProperties[CacheExpireKey] = DateTime.Now.AddMinutes(CacheTimeoutMinutes);
//esite.RootWeb.AllProperties[CacheExpireKey] = DateTime.Now.AddSeconds(30);

esite.RootWeb.AllowUnsafeUpdates = true;
esite.RootWeb.Update();
}
});
}
}


protected virtual string Serialize(TData data)
{
return SerializeInternal(data);
}

protected virtual TData Deserialize(string data)
{
return DeserializeInternal<TData>(data);
}


protected string SerializeInternal(object data)
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(data.GetType());
serializer.Serialize(ms, data);

ms.Flush();
ms.Position = 0;

using (StreamReader sr = new StreamReader(ms))
{
string ret = sr.ReadToEnd();

// remove these lines if you need to strip the xml declaration adn the xsi and xmlns namespaces
//ret = Regex.Replace(ret, @"<\?(.*?)\?>", "");
//ret = Regex.Replace(ret, "xmlns:xsi=\"(.+?)\"", "");
//ret = Regex.Replace(ret, "xmlns:xsd=\"(.+?)\"", "");

return ret;
}
}
}

protected T DeserializeInternal<T>(string data)
{
using (XmlTextReader tr = new XmlTextReader(new StringReader(data)))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
object o = serializer.Deserialize(tr);
return (T)o;
}
}


public virtual void InvalidateCache()
{
var ctx = HttpContext.Current;

if (ctx != null)
ctx.Items[CacheKey] = null;

SPSecurity.RunWithElevatedPrivileges(() =>
{
using (SPSite esite = new SPSite(SiteID))
{
esite.RootWeb.AllProperties[CacheKey] = null;
esite.RootWeb.AllProperties[CacheExpireKey] = null;

esite.RootWeb.AllowUnsafeUpdates = true;
esite.RootWeb.Update();
}
});
}

}
}

Change log

r418 by simon.clint on Sep 12, 2008   Diff
[No log message]
Go to: 
Sign in to write a code review

Older revisions

r417 by simon.clint on Sep 12, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 4938 bytes, 244 lines
Powered by Google Project Hosting