public ReliableQueueTests() { _redis = new FullRedis("127.0.0.1:6379", null, 2); #if DEBUG _redis.Log = XTrace.Log; #endif }
public BasicTest() { FullRedis.Register(); var rds = new FullRedis("127.0.0.1:6379", null, 2); Cache = rds as FullRedis; }
private FullRedis InitFullRedis(RedisMQOptions options) { try { FullRedis fullRedis = null; if (string.IsNullOrEmpty(options.Config)) { fullRedis = new FullRedis(options.Server, options.Password, options.Db); } else { fullRedis = new FullRedis(); fullRedis.Init(options.Config); } fullRedis.Timeout = options.Timeout; return(fullRedis); } catch (Exception e) { _logger.LogError(e, "初始化Redis失败!"); Console.WriteLine(e); throw; } }
public BasicTest() { FullRedis.Register(); var rds = FullRedis.Create("127.0.0.1:6379", 2); Cache = rds as FullRedis; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var server = Stardust.Setting.Current.Server; if (server.IsNullOrEmpty()) { server = "http://star.newlifex.com:6600"; } // APM跟踪器 var tracer = new StarTracer(server) { Log = XTrace.Log }; DefaultTracer.Instance = tracer; ApiHelper.Tracer = tracer; DAL.GlobalTracer = tracer; TracerMiddleware.Tracer = tracer; services.AddSingleton <ITracer>(tracer); // 引入Redis,用于消息队列和缓存,单例,带性能跟踪 var rds = new FullRedis { Tracer = tracer }; rds.Init("server=127.0.0.1:6379;password=;db=3;timeout=5000"); services.AddSingleton <ICache>(rds); services.AddSingleton(rds); services.AddControllers(); }
//private readonly MqttClient _mqtt; //private readonly Producer _producer; public Worker(ILog logger, FullRedis redis /*, MqttClient mqtt, Producer producer*/) { _logger = logger; _redis = redis; //_mqtt = mqtt; //_producer = producer; }
/// <summary> /// 异步方式触发订单相关事件 /// </summary> public static void AsynEventHandle() { Guid userId = Guid.NewGuid(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var order = new OrderModel() { CreateTime = DateTime.Now, Id = Guid.NewGuid(), Money = (decimal)300.00, Number = 1, ProductName = "鲜花一束", UserId = userId }; Console.WriteLine($"模拟存储订单【采取Redis做消息队列的异步方式】"); Thread.Sleep(1000); FullRedis fullRedis = new FullRedis("127.0.0.1:6379", "", 1); //这里尝试过使用redis 的订阅发布模式,在执行发布命令时候发现值但凡出现空格或者"符号则会异常... fullRedis.LPUSH("orders", new OrderModel[] { order }); stopwatch.Stop(); Console.WriteLine($"下单总耗时:{stopwatch.ElapsedMilliseconds}毫秒"); Console.ReadLine(); }
public RedisStatTests() { _redis = new FullRedis("127.0.0.1:6379", null, 5); #if DEBUG _redis.Log = XTrace.Log; #endif }
static void Main(string[] args) { XTrace.UseConsole(); Console.WriteLine("进入Redis消息订阅者模式订单消息推送订阅者客户端!"); EventBus eventBus = new EventBus(); eventBus.EventRegister(typeof(OrderCreateEventNotifyHandle), typeof(OrderCreateEventData)); eventBus.EventRegister(typeof(OrderCreateEventStockLockHandle), typeof(OrderCreateEventData)); FullRedis fullRedis = new FullRedis("127.0.0.1:6379", "", 1); fullRedis.Log = XTrace.Log; fullRedis.Timeout = 30000; OrderModel order = null; while (order == null) { order = fullRedis.BLPOP <OrderModel>("orders", 20); if (order != null) { Console.WriteLine($"得到订单信息:{JsonConvert.SerializeObject(order)}"); //执行相关事件 eventBus.Trigger(new OrderCreateEventData() { Order = order, }); //再次设置为null方便循环读取 order = null; } } Console.ReadLine(); }
public void TraceNode(RedisNode node) { if (!_servers.TryGetValue(node.Id, out var rds)) { _servers[node.Id] = rds = new FullRedis(); } // 可能后面更新了服务器地址和密码 rds.Server = node.Server; rds.Password = node.Password; rds.Tracer = DefaultTracer.Instance; //var inf = rds.GetInfo(true); var inf = rds.GetInfo(false); node.Fill(inf); node.SaveAsync(); var data = new RedisData { RedisId = node.Id, Name = node.Name, }; var dbs = data.Fill(inf); data.Insert(); // 扫描队列 if (node.ScanQueue && dbs != null) { ScanQueue(node, dbs); } }
public static void Proformance() { var redisHelper = new FullRedis("127.0.0.1:6379", "", 2); redisHelper.Log = XTrace.Log; redisHelper.Bench(); }
private static async Task ConsumeAsync(FullRedis redis, String topic, CancellationToken token) { var queue = redis.GetStream <String>(topic); queue.Group = "test"; queue.GroupCreate(queue.Group); while (!token.IsCancellationRequested) { try { var mqMsg = await queue.TakeMessageAsync(10); if (mqMsg != null) { var msg = mqMsg.GetBody <Area>(); XTrace.WriteLine("Consume {0} {1}", msg.Code, msg.Name); queue.Acknowledge(mqMsg.Id); } } catch (Exception ex) { XTrace.WriteException(ex); } } }
public HashTest() { _redis = new FullRedis("127.0.0.1:6379", null, 2); #if DEBUG _redis.Log = XTrace.Log; #endif }
public StreamTests() { //_redis = new FullRedis("127.0.0.1:6379", null, 2); var config = ""; var file = @"config\redis.config"; if (File.Exists(file)) { config = File.ReadAllText(file.GetFullPath())?.Trim(); } if (config.IsNullOrEmpty()) { config = "server=127.0.0.1:6379;db=3"; } if (!File.Exists(file)) { File.WriteAllText(file.EnsureDirectory(true).GetFullPath(), config); } _redis = new FullRedis(); _redis.Init(config); #if DEBUG _redis.Log = NewLife.Log.XTrace.Log; #endif }
static void Test1() { var ic = new FullRedis("127.0.0.1:6379", null, 3); //var ic = new FullRedis(); //ic.Server = "127.0.0.1:6379"; //ic.Db = 3; ic.Log = XTrace.Log; // 简单操作 Console.WriteLine("共有缓存对象 {0} 个", ic.Count); ic.Set("name", "大石头"); Console.WriteLine(ic.Get <String>("name")); var ks = ic.Execute(null, c => c.Execute <String[]>("KEYS", "*")); var keys = ic.Keys; ic.Set("time", DateTime.Now, 1); Console.WriteLine(ic.Get <DateTime>("time").ToFullString()); Thread.Sleep(1100); Console.WriteLine(ic.Get <DateTime>("time").ToFullString()); // 列表 var list = ic.GetList <DateTime>("list"); list.Add(DateTime.Now); list.Add(DateTime.Now.Date); list.RemoveAt(1); Console.WriteLine(list[list.Count - 1].ToFullString()); // 字典 var dic = ic.GetDictionary <DateTime>("dic"); dic.Add("xxx", DateTime.Now); Console.WriteLine(dic["xxx"].ToFullString()); // 队列 var mq = ic.GetQueue <String>("queue"); mq.Add(new[] { "abc", "g", "e", "m" }); var arr = mq.Take(3); Console.WriteLine(arr.Join(",")); // 集合 var set = ic.GetSet <String>("181110_1234"); set.Add("xx1"); set.Add("xx2"); set.Add("xx3"); Console.WriteLine(set.Count); Console.WriteLine(set.Contains("xx2")); Console.WriteLine("共有缓存对象 {0} 个", ic.Count); }
public PubSubTests() { var rds = new FullRedis("127.0.0.1:6379", null, 2); #if DEBUG rds.Log = NewLife.Log.XTrace.Log; #endif _redis = rds; }
public RedisMQConsumerClient(string queueName, IConnectionChannelPool connectionChannelPool, IOptions <RedisMQOptions> options) { _queueName = queueName; _connectionChannelPool = connectionChannelPool; _redisMQOptions = options.Value; _channel = connectionChannelPool.Rent(); _redisReliableQueues = new List <RedisReliableQueue <string> >(); }
static void Main(String[] args) { XTrace.UseConsole(); FullRedis.Register(); Test3(); Console.ReadKey(); }
static void Main(string[] args) { XTrace.UseConsole(); // 激活FullRedis,否则new FullRedis会得到默认的Redis对象 FullRedis.Register(); Test1(); Console.ReadKey(); }
static void Main(String[] args) { XTrace.UseConsole(); // 激活FullRedis,否则Redis.Create会得到默认的Redis对象 FullRedis.Register(); Test5(); Console.ReadKey(); }
static void InitRedis(IObjectContainer services, ITracer tracer) { // 引入 Redis,用于消息队列和缓存,单例,带性能跟踪 var rds = new FullRedis { Tracer = tracer }; rds.Init("server=127.0.0.1:6379;password=;db=3;timeout=5000"); services.AddSingleton <ICache>(rds); services.AddSingleton(rds); }
public static void Start(FullRedis redis) { var topic = "EasyQueue"; // 独立线程消费 var thread = new Thread(s => Consume(redis, topic)); thread.Start(); // 发布消息 Public(redis, topic); }
public void ConfigureContainer(ContainerBuilder builder) { builder.Register(context => { FullRedis.Register(); return(new FullRedis("127.0.0.1:6379", "cash1234", 0)); }).As <ICache>().SingleInstance(); builder.Register(context => Cache.Default) .As <ICache>().SingleInstance() .PreserveExistingDefaults(); }
private static void Consume(FullRedis redis, String topic) { var queue = redis.GetQueue <Area>(topic); while (true) { var msg = queue.TakeOne(10); if (msg != null) { XTrace.WriteLine("Consume {0} {1}", msg.Code, msg.Name); } } }
public static void Start(FullRedis redis) { var topic = "DelayQueue"; // 独立线程消费 var source = new CancellationTokenSource(); Task.Run(() => ConsumeAsync(redis, topic, source.Token)); // 发布消息 Public(redis, topic); source.Cancel(); }
private FullRedis GetOrAdd(RedisNode node, Int32 db) { var key = $"{node.Id}-{db}"; if (!_servers2.TryGetValue(key, out var rds)) { _servers2[key] = rds = new FullRedis(); } rds.Server = node.Server; rds.Password = node.Password; rds.Db = db; return(rds); }
public static void Start(FullRedis redis) { var topic = "FullQueue"; var queue = redis.GetStream <String>(topic); // 独立线程消费 var source = new CancellationTokenSource(); Task.Run(() => ConsumeAsync(redis, topic, source.Token)); // 发布消息 Public(redis, topic); //source.Cancel(); }
/// <summary> /// Adds services for FullRedis to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection. /// </summary> /// <param name="services"></param> /// <param name="config"></param> /// <returns></returns> public static IServiceCollection AddRedis(this IServiceCollection services, String config) { if (String.IsNullOrEmpty(config)) { throw new ArgumentNullException(nameof(config)); } var redis = new FullRedis(); redis.Init(config); services.AddSingleton <Redis>(redis); services.AddSingleton <FullRedis>(redis); return(services); }
private static async Task ConsumeAsync(FullRedis redis, String topic, CancellationToken token) { var queue = redis.GetDelayQueue <String>(topic); while (!token.IsCancellationRequested) { var mqMsg = await queue.TakeOneAsync(10); if (mqMsg != null) { var msg = mqMsg.ToJsonEntity <Area>(); XTrace.WriteLine("Consume {0} {1}", msg.Code, msg.Name); queue.Acknowledge(mqMsg); } } }
private static void Public(FullRedis redis, String topic) { var queue = redis.GetStream <Area>(topic); queue.Add(new Area { Code = 110000, Name = "北京市" }); Thread.Sleep(1000); queue.Add(new Area { Code = 310000, Name = "上海市" }); Thread.Sleep(1000); queue.Add(new Area { Code = 440100, Name = "广州市" }); Thread.Sleep(1000); }