Skip to content

Commit

Permalink
Changed from ServiceStack to StackExchange.Redis
Browse files Browse the repository at this point in the history
ServiceStack started a commercial license with their newest version with
low limits on usage of the free version.  So we switch to the free
StackExchange.Redis
  • Loading branch information
Jeff Treuting committed Oct 17, 2014
1 parent 28e4b2d commit 92d3a4c
Show file tree
Hide file tree
Showing 196 changed files with 128,306 additions and 4,570 deletions.
84 changes: 60 additions & 24 deletions SharpRepository.Caching.Redis/RedisCachingProvider.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,83 @@
using System;
using System.Runtime.Caching;
using ServiceStack.Redis;
using SharpRepository.Repository.Caching;
using StackExchange.Redis;

namespace SharpRepository.Caching.Redis
{
public class RedisCachingProvider : ICachingProvider
{
protected RedisClient Client { get; set; }
protected IDatabase Redis { get; set; }

public RedisCachingProvider() : this(new RedisClient())
public RedisCachingProvider() : this("localhost")
{
}

public RedisCachingProvider(string host)
public RedisCachingProvider(string host, bool ssl = true)
{
if (String.IsNullOrEmpty(host)) throw new ArgumentNullException("host");

Client = new RedisClient(host);
var configOptions = new ConfigurationOptions
{
EndPoints =
{
{host}
},
Ssl = ssl
};

Initialize(configOptions);
}

public RedisCachingProvider(string host, int port)
public RedisCachingProvider(string host, int port, bool ssl = true)
{
if (String.IsNullOrEmpty(host)) throw new ArgumentNullException("host");

Client = new RedisClient(host, port);
var configOptions = new ConfigurationOptions
{
EndPoints =
{
{host, port}
},
Ssl = ssl
};

Initialize(configOptions);
}

public RedisCachingProvider(string host, int port, string password)
public RedisCachingProvider(string host, int port, string password, bool ssl =true)
{
if (String.IsNullOrEmpty(host)) throw new ArgumentNullException("host");

Client = new RedisClient(host, port, password);
var configOptions = new ConfigurationOptions
{
EndPoints =
{
{ host, port}
},
Ssl = ssl,
Password = password
};

Initialize(configOptions);
}

public RedisCachingProvider(RedisClient client)
public RedisCachingProvider(ConfigurationOptions configOptions)
{
if (client == null) throw new ArgumentNullException("client");
if (configOptions == null) throw new ArgumentNullException("configOptions");

Client = client;
Initialize(configOptions);
}

private void Initialize(ConfigurationOptions configOptions)
{
if (RedisConnector.Connection == null)
{
configOptions.AbortOnConnectFail = false;
RedisConnector.Connection = ConnectionMultiplexer.Connect(configOptions);
}

Redis = RedisConnector.Connection.GetDatabase();
}

/// <summary>
Expand All @@ -54,15 +93,13 @@ public void Set<T>(string key, T value, CacheItemPriority priority = CacheItemPr
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");

TimeSpan? expiry = null;
if (timeoutInSeconds.HasValue)
{
Client.Set(key, value, new TimeSpan(0, 0, 0, timeoutInSeconds.Value));
}
else
{
Client.Set(key, value);
expiry = new TimeSpan(0, 0, 0, timeoutInSeconds.Value);
}


Redis.Set(key, value, expiry);
}

/// <summary>
Expand All @@ -73,7 +110,7 @@ public void Clear(string key)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");

Client.Remove(key);
Redis.KeyDelete(key);
}

/// <summary>
Expand All @@ -85,7 +122,7 @@ public bool Exists(string key)
{
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");

return (Client.Get(key) != null);
return Redis.KeyExists(key);
}

/// <summary>
Expand All @@ -102,7 +139,7 @@ public bool Get<T>(string key, out T value)

try
{
value = Client.Get<T>(key);
value = Redis.Get<T>(key);

if (Equals(value, default(T)))
{
Expand All @@ -124,13 +161,12 @@ public int Increment(string key, int defaultValue, int value, CacheItemPriority
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException("key");

// no need to use a lock since the redis increment method is atomic already
return Convert.ToInt32(Client.Increment(key, Convert.ToUInt32(value)));
return Convert.ToInt32(Redis.StringIncrement(key, value));
}

public void Dispose()
{
Client.Dispose();
Client = null;

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SharpRepository.Repository.Configuration;
using System;
using SharpRepository.Repository.Configuration;

namespace SharpRepository.Caching.Redis
{
Expand All @@ -14,12 +15,13 @@ public RedisCachingProviderConfiguration(string name, string host, int port) : t
{
}

public RedisCachingProviderConfiguration(string name, string host, int port, string password)
public RedisCachingProviderConfiguration(string name, string host, int port, string password, bool ssl = true)
{
Name = name;
Host = host;
Port = port;
Password = password;
Ssl = ssl;
Factory = typeof (RedisConfigCachingProviderFactory);
}

Expand All @@ -37,5 +39,10 @@ public string Password
{
set { Attributes["password"] = value; }
}

public bool Ssl
{
set { Attributes["ssl"] = value.ToString(); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public RedisConfigCachingProviderFactory(ICachingProviderConfiguration config)
public override ICachingProvider GetInstance()
{
int port;
bool ssl;

if (!Boolean.TryParse(CachingProviderConfiguration["ssl"], out ssl))
{
ssl = true;
}

// this seems like a dumb way to do this :)
if (!String.IsNullOrEmpty(CachingProviderConfiguration["password"]))
Expand All @@ -23,17 +29,17 @@ public override ICachingProvider GetInstance()
throw new ArgumentException("port");
}

return new RedisCachingProvider(CachingProviderConfiguration["host"], port, CachingProviderConfiguration["password"]);
return new RedisCachingProvider(CachingProviderConfiguration["host"], port, CachingProviderConfiguration["password"], ssl);
}

if (Int32.TryParse(CachingProviderConfiguration["port"], out port))
{
return new RedisCachingProvider(CachingProviderConfiguration["host"], port);
return new RedisCachingProvider(CachingProviderConfiguration["host"], port, ssl);
}

if (!String.IsNullOrEmpty(CachingProviderConfiguration["host"]))
{
return new RedisCachingProvider(CachingProviderConfiguration["host"]);
return new RedisCachingProvider(CachingProviderConfiguration["host"], ssl);
}

return new RedisCachingProvider();
Expand Down
9 changes: 9 additions & 0 deletions SharpRepository.Caching.Redis/RedisConnector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using StackExchange.Redis;

namespace SharpRepository.Caching.Redis
{
public static class RedisConnector
{
public static ConnectionMultiplexer Connection { get; set; }
}
}
40 changes: 29 additions & 11 deletions SharpRepository.Caching.Redis/SharpRepository.Caching.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,44 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ServiceStack.Common, Version=4.0.32.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Common.4.0.32\lib\net40\ServiceStack.Common.dll</HintPath>
<Reference Include="Microsoft.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Interfaces, Version=4.0.0.0, Culture=neutral, PublicKeyToken=e06fbc6124f57c43, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Interfaces.4.0.32\lib\portable-wp80+sl5+net40+win8+monotouch+monoandroid\ServiceStack.Interfaces.dll</HintPath>
<Reference Include="Microsoft.Threading.Tasks.Extensions">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Redis, Version=4.0.32.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Redis.4.0.32\lib\net40\ServiceStack.Redis.dll</HintPath>
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text, Version=4.0.32.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Text.4.0.32\lib\net40\ServiceStack.Text.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.6.0.5\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="StackExchange.Redis">
<HintPath>..\packages\StackExchange.Redis.1.0.333\lib\net40\StackExchange.Redis.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IO">
<HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\net40\System.IO.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Runtime">
<HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\net40\System.Runtime.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\net40\System.Threading.Tasks.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RedisCachingProvider.cs" />
<Compile Include="RedisCachingProviderConfiguration.cs" />
<Compile Include="RedisConfigCachingProviderFactory.cs" />
<Compile Include="RedisConnector.cs" />
<Compile Include="StackExchangeExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
Expand All @@ -65,9 +77,15 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
44 changes: 44 additions & 0 deletions SharpRepository.Caching.Redis/StackExchangeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace SharpRepository.Caching.Redis
{
public static class StackExchangeRedisExtensions
{
public static T Get<T>(this IDatabase cache, string key)
{
return Deserialize<T>(cache.StringGet(key));
}

public static object Get(this IDatabase cache, string key)
{
return Deserialize<object>(cache.StringGet(key));
}

public static void Set(this IDatabase cache, string key, object value, TimeSpan? expiry = null)
{
cache.StringSet(key, Serialize(value), expiry);
}

static string Serialize(object o)
{
if (o == null)
{
return null;
}

return JsonConvert.SerializeObject(o);
}

static T Deserialize<T>(string value)
{
if (value == null)
{
return default(T);
}

return JsonConvert.DeserializeObject<T>(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<sharpRepository>
<cachingProviders>
<cachingProvider name="redisProvider" host="127.0.0.1" port="6379" factory="SharpRepository.Caching.Redis.RedisConfigCachingProviderFactory, SharpRepository.Caching.Redis" />
<cachingProvider name="redisProvider" host="127.0.0.1" port="6379" ssl="True" factory="SharpRepository.Caching.Redis.RedisConfigCachingProviderFactory, SharpRepository.Caching.Redis" />
</cachingProviders>
</sharpRepository>

Expand Down
15 changes: 15 additions & 0 deletions SharpRepository.Caching.Redis/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
9 changes: 5 additions & 4 deletions SharpRepository.Caching.Redis/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ServiceStack.Common" version="4.0.32" targetFramework="net40" />
<package id="ServiceStack.Interfaces" version="4.0.32" targetFramework="net40" />
<package id="ServiceStack.Redis" version="4.0.32" targetFramework="net40" />
<package id="ServiceStack.Text" version="4.0.32" targetFramework="net40" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net40" />
<package id="StackExchange.Redis" version="1.0.333" targetFramework="net40" />
</packages>
Loading

0 comments on commit 92d3a4c

Please sign in to comment.