敢为自己目标行动的人,整个世界都会为你让路
Program.cs主程序:
class Program
{
static void Main(string[] args)
{
List<User> userList =new List<User>() {
new User { UserName = "chengjun", PassWord = "qwerty" },
new User { UserName = "chengjun1", PassWord = "qwerty" },
new User { UserName = "chengjun2", PassWord = "qwerty" },
new User { UserName = "chengjun3", PassWord = "qwerty" },
new User { UserName = "chengjun5", PassWord = "qwerty" },
new User { UserName = "chengjun6", PassWord = "qwerty" },
new User { UserName = "chengjun7", PassWord = "qwerty" },
new User { UserName = "chengjun8", PassWord = "qwerty" }
};
var stackExchangeHelper = new StackExchangeHelper();
stackExchangeHelper.Set("xxoo_cus", userList);
var getUserListFromRedis = stackExchangeHelper.Get<List<User>>("xxoo_cus");
Console.WriteLine("List元素数量:"+getUserListFromRedis.Count);
Console.ReadKey();
}
}
[Serializable]
public class User
{
public string UserName { get; set; }
public string PassWord { get; set; }
}
PubConstant.cs代码:
public class PubConstant
{
public static string RedisIp
{
get
{
string _redisIp = ConfigurationManager.AppSettings["RedisIp"];
return _redisIp;
}
}
public static string RedisPort
{
get
{
string _redisPort = ConfigurationManager.AppSettings["RedisPort"];
return _redisPort;
}
}
public static string RedisPass
{
get
{
string _redisPass=ConfigurationManager.AppSettings["RedisPass"];
return _redisPass;
}
}
}
App.config代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="RedisIp" value="127.0.0.1" />
<add key="RedisPort" value="6379" />
<add key="RedisPass" value="123456" />
</appSettings>
</configuration>
StackExchangeConn.cs代码:
public class StackExchangeConn
{
private static ConnectionMultiplexer _connection;
private static readonly object SyncObject = new object();
public static ConnectionMultiplexer GetFactionConn
{
get
{
if (_connection == null || !_connection.IsConnected)
{
lock (SyncObject)
{
var configurationOptions = new ConfigurationOptions()
{
Password = PubConstant.RedisPass,
EndPoints = { { PubConstant.RedisIp, Convert.ToInt32(PubConstant.RedisPort) } }
};
_connection = ConnectionMultiplexer.Connect(configurationOptions);
}
}
return _connection;
}
}
}
StackExchangeHelper.cs助手类
public class StackExchangeHelper
{
private static IDatabase cache;
public StackExchangeHelper()
{
cache = StackExchangeConn.GetFactionConn.GetDatabase();
}
public bool StringSet(string key, string value)
{
return cache.StringSet(key, value);
}
public string StringGet(string key)
{
return cache.StringGet(key);
}
public int IntGet(string key)
{
try
{
return Convert.ToInt32(StringGet(key));
}
catch (Exception)
{
return (-9999);
}
}
public T Get<T>(string key)
{
return Deserialize<T>(cache.StringGet(key));
}
public object Get(string key)
{
return Deserialize<object>(cache.StringGet(key));
}
public void Set(string key, object value)
{
cache.StringSet(key, Serialize(value));
}
static byte[] Serialize(object o)
{
if (o == null)
{
return null;
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, o);
byte[] objectDataAsStream = memoryStream.ToArray();
return objectDataAsStream;
}
}
static T Deserialize<T>(byte[] stream)
{
if (stream == null)
{
return default(T);
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(stream))
{
T result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}
}
运行结果:

