Redis開發學習(4)_StackExchange.Redis_進行String相關操作




在一些資料整理上有提到雖然目前針對.NET
釋出相關Redis Client API  不只一個StackExchanged.Redis
但是ServiceStack是要付費的!!!!!!!
而且在舊新版本的切換上據說語法也會有不相容的問題



(1)資料塞入指令

寫法1.透過StringSet


1
IDatabaseObject.StringSet("Key","value");


寫法2.透過Execute方式


1
IDatabaseObject.Execute("SET", "Key", "Value");



程式碼

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            IDatabase myDb = myConn.GetDatabase();
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("目前的keys");
            int i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i,key);
            }
            Console.WriteLine(new string('=',50));
            Console.WriteLine("塞入一筆String Record(寫法1).StringSet");
            myDb.StringSet("fruitP","Peach");
            i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }
            Console.WriteLine("塞入一筆String Record(寫法2).Execute");
            myDb.Execute("SET", "fruitD", "Durian");
            i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }



            Console.ReadKey();
        }
    }
}





(2)資料刪除指令

寫法1.

1
IDatabaseObject.Execute("DEL", "Key", "Value");



程式碼
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            IDatabase myDb = myConn.GetDatabase();
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("目前的keys");
            int i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i,key);
            }
            Console.WriteLine(new string('=',50));
           
            Console.WriteLine("刪除一筆String Record");
            myDb.Execute("DEL", "fruitD", "Durian");
            i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }
            


            Console.ReadKey();
        }
    }
}


寫法2.


1
IDatabaseObject.KeyDelete("Key");



程式碼


 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            IDatabase myDb = myConn.GetDatabase();
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("目前的keys");
            int i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("刪除一筆String Record");
            myDb.KeyDelete("0.3");
            i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }
            Console.WriteLine("刪除多筆String Record");
            RedisKey[] delKeys = new RedisKey[] { "7", "order" };
            myDb.KeyDelete(delKeys);
            i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }

            Console.ReadKey();
        }
    }
}


(3)鍵相應值獲取

1
IDatabaseObject.StringGet("Key")




程式碼


 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            IDatabase myDb = myConn.GetDatabase();
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("目前的keys");
            int i = 0;
            foreach (var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys())
            {
                i++;
                Console.WriteLine("key{0}:{1}", i, key);
            }
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("獲取Key對應value為:{0}" , myDb.StringGet("fruitP"));

            RedisKey[] keyArr = new RedisKey[] { "fruitP", "fruitB" };
            Console.WriteLine("一次獲取多個Key各自對應的value:" + string.Join(",",myDb.StringGet(keyArr).ToArray()));


            Console.ReadKey();
        }
    }
}


(4)鍵值相應value串接


1
IDatabaseObject.StringAppend("Key","appendvalue");



程式碼


 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            IDatabase myDb = myConn.GetDatabase();
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("獲取Key對應value為:{0}", myDb.StringGet("fruitB"));

            myDb.StringAppend("fruitB"," Juice");
            Console.WriteLine("獲取Key對應value為:{0}", myDb.StringGet("fruitB"));


            Console.ReadKey();
        }
    }
}




(5)鍵值相應value的字串長


1
IDatabaseObject.StringLength("Key");




程式碼


 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            IDatabase myDb = myConn.GetDatabase();
            Console.WriteLine(new string('=', 50));

            Console.WriteLine("獲取Key對應value為:{0}", myDb.StringGet("fruitB"));


            Console.WriteLine("Key對應value的字串長:{0}", myDb.StringLength("fruitB"));


            Console.ReadKey();
        }
    }
}









留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header