Let's finally write some code. First take a quick look at MonoX Cache provider. The default MonoX cache provider inherits
, so we'll also start there.
namespace
MonoSoftware.Web.Caching
{
public
interface
ICacheProvider
{
CacheItemPriorityLevel Priortiy {
get
;
set
; }
int
Timeout {
get
;
set
; }
T Get<T>(
string
key);
object
Get(
string
key);
void
Remove(
string
key);
void
RemoveAll(
string
key);
void
Store(
string
key,
object
data);
}
}
Our SimpleAzureCacheProvider will inherit this interface and we will implement its properties and methods.
001.
namespace
MyProject
002.
{
003.
public
class
SimpleAzureCacheProvider : ICacheProvider
004.
{
005.
006.
#region Fields
007.
protected
static
object
padLock =
new
object
();
008.
protected
static
object
padLockInit =
new
object
();
009.
#endregion
010.
011.
#region Properties
012.
public
string
CacheName {
get
;
set
; }
013.
014.
protected
static
volatile DataCacheFactory _factory =
null
;
015.
protected
static
volatile DataCache _cache =
null
;
016.
017.
protected
virtual
DataCache Cache
018.
{
019.
get
020.
{
021.
if
(_cache ==
null
)
022.
{
023.
lock
(padLockInit)
024.
{
025.
if
(_cache ==
null
)
026.
{
027.
DataCacheFactoryConfiguration configuration =
new
DataCacheFactoryConfiguration();
028.
_factory =
new
DataCacheFactory(configuration);
029.
_cache = _factory.GetCache(
"cachename"
);
030.
}
031.
}
032.
}
033.
return
_cache;
034.
}
035.
private
set
{ _cache = value; }
036.
}
037.
038.
private
int
_timeout = 10;
039.
public
int
Timeout
040.
{
041.
get
{
return
_timeout; }
042.
set
{ _timeout = value; }
043.
}
044.
045.
private
CacheItemPriorityLevel _priortiy = CacheItemPriorityLevel.AboveNormal;
046.
public
CacheItemPriorityLevel Priortiy
047.
{
048.
get
{
return
_priortiy; }
049.
set
{ _priortiy = value; }
050.
}
051.
#endregion
052.
053.
#region Constructor
054.
public
AzureCacheProvider()
055.
{
056.
057.
}
058.
#endregion
059.
060.
#region Methods
061.
062.
public
T Get<T>(
string
key)
063.
{
064.
object
item = Get(key);
065.
if
(item !=
null
)
066.
{
067.
return
item
is
T ? (T)item :
default
(T);
068.
}
069.
else
070.
return
default
(T);
071.
}
072.
073.
public
object
Get(
string
key)
074.
{
075.
return
Cache.Get(key);
076.
}
077.
078.
public
void
Store(
string
key,
object
data)
079.
{
080.
if
(data !=
null
)
081.
Cache.Put(key, data);
082.
}
083.
084.
public
void
Remove(
string
key)
085.
{
086.
lock
(padLock)
087.
{
088.
Cache.Remove(key);
089.
}
090.
}
091.
092.
public
void
RemoveAll(
string
key)
093.
{
094.
lock
(padLock)
095.
{
096.
Remove(key);
097.
}
098.
}
099.
100.
#endregion
101.
102.
}
103.
}
As you can see, the implementation part is really simple. If you need more information on Azure Cache, please follow this