First, download the third-party class library: Stackexchange.redis
NuGet Stackexchange.redis, click Install, add a third-party namespace: using Stackexchange.redis;
Second, the use of Stackexchange.redis (using the premise is Redis server: installation of Linux Redis can see this article):
1, the key value of access:
Static voidNoth (string[] args) { stringHost ="192.168.117.129"; Connectionmultiplexer Redis=Connectionmultiplexer.connect (host); Idatabase DB=Redis. Getdatabase (); Db. Stringset ("User","{name:\ "tom\"}"); Db. Stringappend ("User",", {name:\ "Jack\"}"); stringuser = db. Stringget ("User"); Console.WriteLine (user); Console.read (); }
2, the subscription of the event:
Publish-side code:
Static voidMain (string[] args) { stringHost ="192.168.117.129"; Connectionmultiplexer Redis=Connectionmultiplexer.connect (host); Isubscriber DB=Redis. Getsubscriber (); Db. Publish ("C1","123"); stringReader="Start End"; while(Reader! ="Exit") {Reader=Console.ReadLine (); Db. Publish ("C1", reader); } console.read (); }
Subscriber Code:
Static voidMain (string[] args) { stringHost ="192.168.117.129"; Connectionmultiplexer Redis=Connectionmultiplexer.connect (host); Isubscriber DB=Redis. Getsubscriber (); Db. Subscribe ("C1",NewAction<redischannel,redisvalue> ((chan,msage) ={Console.WriteLine ("Channel:"+Chan); Console.WriteLine ("Message content:"+msage); })); Console.ReadLine (); }
Use of the Redis C # cache