RedisBase.cs
2.84 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
113
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using FreeRedis;
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Newtonsoft.Json;
namespace Hh.Mes.Common.Redis
{
public class RedisBase
{
public RedisClient redisClient { get; set; }
/// <summary>
/// 1天
/// </summary>
public int dayTime = 1 * 24 * 60 * 60;
/// <summary>
/// 2天
/// </summary>
public int dayTimeTwo = 2 * 24 * 60 * 60;
/// <summary>
/// 20 分钟
/// </summary>
public int minutes20 = 20 * 60;
public string redisIp { get; set; }
public RedisBase()
{
redisIp = ConfigRead.GetInstance.GetAppsetConnection().RedisIp;
redisClient = new RedisClient(redisIp);
}
public bool IsStartRedisServer()
{
try
{
redisClient.Set("projectKey", ConfigRead.GetInstance.GetAppsetConnection().LoginTitle);
return true;
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
var msg = "redis server请检查服务是否开启,ip和密码是否正确 :" + ConfigRead.GetInstance.GetAppsetConnection().RedisIp;
Console.WriteLine(msg);
Log4NetHelper.Instance.Info(msg);
Console.ResetColor();
Thread.Sleep(8000);
return false;
}
}
public bool ExistsKey(string keys)
{
return redisClient.Exists(keys);
}
public void Set(string keys, string json)
{
redisClient.Set(keys, json);
}
/// <summary>
/// time 秒
/// </summary>
/// <param name="keys"></param>
/// <param name="json"></param>
/// <param name="time"></param>
public void Set(string keys, string json, int time)
{
redisClient.Set(keys, json, time);
}
public void SetT<T>(string keys, T json, int time)
{
var val = JsonConvert.SerializeObject(json);
redisClient.Set(keys, val, time);
}
public T GetT<T>(string keys)
{
var temp = redisClient.Get(keys);
if (temp == null)
{
return default;
}
else
{
return JsonConvert.DeserializeObject<T>(temp);
}
}
public string GetString(string keys)
{
var temp = redisClient.Get(keys);
if (temp == null)
{
return default;
}
else
{
return temp;
}
}
}
}