forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestExtensions.cs
More file actions
112 lines (97 loc) · 3.68 KB
/
Copy pathRequestExtensions.cs
File metadata and controls
112 lines (97 loc) · 3.68 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
using System;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Web;
namespace ServiceStack
{
public static class RequestExtensions
{
public static AuthUserSession ReloadSession(this IRequest request)
{
return request.GetSession() as AuthUserSession;
}
public static string GetCompressionType(this IRequest request)
{
if (request.RequestPreferences.AcceptsDeflate)
return CompressionTypes.Deflate;
if (request.RequestPreferences.AcceptsGzip)
return CompressionTypes.GZip;
return null;
}
public static string GetHeader(this IRequest request, string headerName)
{
return request.Headers.Get(headerName);
}
/// <summary>
/// Returns the optimized result for the IRequestContext.
/// Does not use or store results in any cache.
/// </summary>
/// <param name="request"></param>
/// <param name="dto"></param>
/// <returns></returns>
public static object ToOptimizedResult<T>(this IRequest request, T dto)
{
string serializedDto = HostContext.ContentTypes.SerializeToString(request, dto);
var compressionType = request.GetCompressionType();
if (compressionType == null)
return (object)serializedDto;
byte[] compressedBytes = serializedDto.Compress(compressionType);
return new CompressedResult(compressedBytes, compressionType, request.ResponseContentType);
}
/// <summary>
/// Overload for the <see cref="ContentCacheManager.Resolve"/> method returning the most
/// optimized result based on the MimeType and CompressionType from the IRequestContext.
/// </summary>
public static object ToOptimizedResultUsingCache<T>(
this IRequest requestContext, ICacheClient cacheClient, string cacheKey,
Func<T> factoryFn)
{
return requestContext.ToOptimizedResultUsingCache(cacheClient, cacheKey, null, factoryFn);
}
/// <summary>
/// Overload for the <see cref="ContentCacheManager.Resolve"/> method returning the most
/// optimized result based on the MimeType and CompressionType from the IRequestContext.
/// <param name="expireCacheIn">How long to cache for, null is no expiration</param>
/// </summary>
public static object ToOptimizedResultUsingCache<T>(
this IRequest requestContext, ICacheClient cacheClient, string cacheKey,
TimeSpan? expireCacheIn, Func<T> factoryFn)
{
var cacheResult = cacheClient.ResolveFromCache(cacheKey, requestContext);
if (cacheResult != null)
return cacheResult;
cacheResult = cacheClient.Cache(cacheKey, factoryFn(), requestContext, expireCacheIn);
return cacheResult;
}
/// <summary>
/// Clears all the serialized and compressed caches set
/// by the 'Resolve' method for the cacheKey provided
/// </summary>
/// <param name="requestContext"></param>
/// <param name="cacheClient"></param>
/// <param name="cacheKeys"></param>
public static void RemoveFromCache(
this IRequest requestContext, ICacheClient cacheClient, params string[] cacheKeys)
{
cacheClient.ClearCaches(cacheKeys);
}
/// <summary>
/// Store an entry in the IHttpRequest.Items Dictionary
/// </summary>
public static void SetItem(this IRequest httpReq, string key, object value)
{
if (httpReq == null) return;
httpReq.Items[key] = value;
}
/// <summary>
/// Get an entry from the IHttpRequest.Items Dictionary
/// </summary>
public static object GetItem(this IRequest httpReq, string key)
{
if (httpReq == null) return null;
object value;
httpReq.Items.TryGetValue(key, out value);
return value;
}
}
}