| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using ServiceStack.Redis;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CarLocalMeter
- {
- public class RedisCls
- {
- public static RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort);//如果有密码则带密码,如果数据存在在db0外的比如db1则第四位为1
- Log lg = Log.GetInstance();
- public static List<string> getValue(out bool flag)
- {
- try
- {
- flag = true;
- return client.GetAllKeys();
- }
- catch
- {
- flag = false;
- return new List<string>();
- }
- }
- /// <summary>
- /// 获取redis数据
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static T getValue<T>(string key, out bool flag)
- {
- try
- {
- flag = true;
- return client.Get<T>(key);
- }
- catch
- {
- flag = false;
- return default(T);
- }
- }
- /// <summary>
- /// 添加redis数据
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value">可以是Json的字符串,也可以是List集合等</param>
- public static void setVaule<T>(string key, T value, out bool flag, DateTime dt = default)
- {
- try
- {
- flag = true;
- if (dt != null && dt > DateTime.Now)
- {
- client.Set<T>(key, value, dt);
- //client.ExpireEntryAt(key, DateTime.Now.AddSeconds(60));
- //client.ExpireEntryIn(key, TimeSpan.FromSeconds(20));
- }
- else
- client.Set<T>(key, value);
- }
- catch
- {
- flag = false;
- }
- }
- public static void setItemVaule(string key, string value, out bool flag, DateTime dt = default)
- {
- try
- {
- flag = true;
- if (dt != null && dt > DateTime.Now)
- {
- client.AddItemToSet(key, value);
- client.ExpireEntryAt(key, dt);
- //client.ExpireEntryIn(key, TimeSpan.FromSeconds(20));
- }
- else
- client.AddItemToSet(key, value);
- }
- catch
- {
- flag = false;
- }
- }
- public static void remoeItemValue(string key, string item, out bool flag)
- {
- try
- {
- flag = true;
- client.RemoveItemFromSet(key, item);
- }
- catch
- {
- flag = false;
- }
- }
- }
- }
|