zoukankan      html  css  js  c++  java
  • Redis C#缓存的使用

    一、下载第三方类库:StackExchange.Redis

      Nuget收索StackExchange.Redis,点击安装即可,新增的第三方命名空间:using StackExchange.Redis;

    二、StackExchange.Redis使用(使用前提是有Redis服务端:Linux  Redis的安装可查看此文):

      1、键值的存取:

    static void Noth(string[] args)
            {
                string host = "192.168.117.129";
                ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);
                IDatabase db = redis.GetDatabase();
                db.StringSet("User", "{name:"TOM"}");
                db.StringAppend("User", ",{name:"JACK"}");
    
                string user = db.StringGet("User");
                Console.WriteLine(user);
                Console.Read();
    
            }

         2、事件的订阅:

      发布端代码:

      

    static void Main(string[] args)
            {
                string host = "192.168.117.129";
                ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);
                ISubscriber db = redis.GetSubscriber();
                db.Publish("c1", "123");
                string reader="start end";
                while (reader != "exit")
                {
                    reader = Console.ReadLine();
                    db.Publish("c1", reader);
                }           
                Console.Read();
    
            }

    订阅者代码:

    static void Main(string[] args)
            {
                string host = "192.168.117.129";
                ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);
                ISubscriber db = redis.GetSubscriber();
                db.Subscribe("c1",new Action<RedisChannel,RedisValue>((chan,msage)=>{
                    Console.WriteLine("通道:"+chan);
                    Console.WriteLine("消息内容:"+msage);
                }));
                Console.ReadLine();
            }
  • 相关阅读:
    IOS开发博客学习网址
    xmpp学习xmpp概述
    java数据库编程之高级查询
    html基础知识笔记
    深入c#编程
    c#入门基础笔记
    java数据库编程之数据库的设计
    小组会谈(2019.3.14)
    软件工程小组问世第四章之需求规格说明书青铜篇
    小组会谈(2019.03.29)
  • 原文地址:https://www.cnblogs.com/xibei666/p/5860531.html
Copyright © 2011-2022 走看看