c#之Redis實踐list,hashtable

寫在前面

最近公司搞了一個活動,用到了redis的隊列,就研究了下redis的相關內容。也順手作了個demo。redis

C#之使用Redis

能夠經過Nuget安裝Reidis的相關程序集。安裝以後發現會引入如下幾個dll測試

一些list,隊列和hashtable的操做。spa

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NServiceKit.Redis;
using Newtonsoft.Json;
namespace RedisDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立redis工廠
            RedisClientFactory factory = RedisClientFactory.Instance;
            //經過工廠建立redisclient對象
            RedisClient client = factory.CreateRedisClient("192.168.1.37", 6379);
            //在list中添加鍵值對
            client.AddItemToList("test_listId", "wolfy");
            //經過鍵和索引取值
            string value = client.GetItemFromList("test_listId", 0);
            //隊列
            for (int i = 0; i < 10; i++)
            {
                client.EnqueueItemOnList("queue_test", "test" + i.ToString());
            }
            while (client.GetListCount("queue_test") > 0)
            {
                Console.WriteLine(client.DequeueItemFromList("queue_test"));
            }
            //hashtable
            for (int i = 0; i < 10; i++)
            {
                client.SetEntryInHash("hashtable_test", "test" + i.ToString(), JsonConvert.SerializeObject(new
                {
                    id = i + 1,
                    name = "wolfy" + i.ToString()
                }));
            }
            //獲取hashtable中的值
            List<string> lst = client.GetHashValues("hashtable_test");
            foreach (var item in lst)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

測試code

總結

關於redis的文章網上有不少,這裏因爲工做須要,先着手弄了個demo,先跑起來,而後再深刻的研究。對象

相關文章
相關標籤/搜索