发布:2021/11/6 17:16:07作者:管理员 来源:本站 浏览次数:1041
参考博客https://www.cnblogs.com/qukaicheng/p/7514168.html写的
安装教程https://www.redis.net.cn/tutorial/3503.html
实现后的东西
跳到H盘;cd打开指定文件夹
执行命令:redis-server.exe redis.conf
这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。
切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 。
设置键值对 set myKey abc
取出键值对 get myKey
然后设置密码
获取密码 :127.0.0.1:6379> CONFIG get requirepas
设置密码:127.0.0.1:6379> CONFIG set requirepass "123"
然后连接后,验证密码:127.0.0.1:6379> AUTH "123";然后才能进行操作
结果如下:设置密码要重新验证:不然config get requirepass 找不到
c# 中应用:添加引用 ServiceStack.Redis 3.9.x Complete Library;代码是复制的
public class RedisCacheHelper
{
private static readonly PooledRedisClientManager pool = null;
private static readonly string[] writeHosts = null;
private static readonly string[] readHosts = null;
public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);
public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
static RedisCacheHelper()
{
var redisWriteHost = ConfigurationManager.AppSettings["redis_server_write"];
var redisReadHost = ConfigurationManager.AppSettings["redis_server_read"];
if (!string.IsNullOrEmpty(redisWriteHost))
{
writeHosts = redisWriteHost.Split(',');
readHosts = redisReadHost.Split(',');
if (readHosts.Length > )
{
pool = new PooledRedisClientManager(writeHosts, readHosts,
new RedisClientManagerConfig()
{
MaxWritePoolSize = RedisMaxWritePool,
MaxReadPoolSize = RedisMaxReadPool,
AutoStart = true
});
}
}
}
public static void Add<T>(string key, T value, DateTime expiry)
{
if (value == null)
{
return;
}
if (expiry <= DateTime.Now)
{
Remove(key);
return;
}
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
r.Set(key, value, expiry - DateTime.Now);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
}
}
public static void Add<T>(string key, T value)
{
RedisCacheHelper.Add<T>(key, value, DateTime.Now.AddMinutes());
}
public static void Add<T>(string key, T value, TimeSpan slidingExpiration)
{
if (value == null)
{
return;
}
if (slidingExpiration.TotalSeconds <= )
{
Remove(key);
return;
}
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
r.Set(key, value, slidingExpiration);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
}
}
public static T Get<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
return default(T);
}
T obj = default(T);
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
obj = r.Get<T>(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", key);
}
return obj;
}
public static void Remove(string key)
{
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
r.Remove(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key);
}
}
public static bool Exists(string key)
{
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
return r.ContainsKey(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key);
}
return false;
}
public static IDictionary<string, T> GetAll<T>(IEnumerable<string> keys) where T : class
{
if (keys == null)
{
return null;
}
keys = keys.Where(k => !string.IsNullOrWhiteSpace(k));
if (keys.Count() == )
{
T obj = Get<T>(keys.Single());
if (obj != null)
{
return new Dictionary<string, T>() { { keys.Single(), obj } };
}
return null;
}
if (!keys.Any())
{
return null;
}
try
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
return r.GetAll<T>(keys);
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", keys.Aggregate((a, b) => a + "," + b));
}
return null;
}
}
配置文件加了密码的设置
源码是柘木写的:
/// <summary>
/// IP地址中可以加入auth验证 password@ip:port
/// </summary>
/// <param name="hosts"></param>
/// <returns></returns>
public static List<RedisEndpoint> ToRedisEndPoints(this IEnumerable<string> hosts)
{
if (hosts == null) return new List<RedisEndpoint>();
//redis终结点的列表
var redisEndpoints = new List<RedisEndpoint>();
foreach (var host in hosts)
{
RedisEndpoint endpoint;
string[] hostParts;
if (host.Contains("@"))
{
hostParts = host.SplitOnLast('@');
var password = hostParts[];
hostParts = hostParts[].Split(':');
endpoint = GetRedisEndPoint(hostParts);
endpoint.Password = password;
}
else
{
hostParts = host.Split(':');
endpoint = GetRedisEndPoint(hostParts);
}
redisEndpoints.Add(endpoint);
}
return redisEndpoints;
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4