ServiceStack.Redis

什么是Redis

首先,简述一下什么是Redis。

Redis是一个开源、支持网络、基于内存、键值对存储数据库,使用ANSI C编写。从2013年5月开始,Redis的开发由Pivotal赞助。在这之前,其开发由VMware赞助。根据月度排行网站DB-Engines.com的数据显示,Redis是最流行的键值对存储数据库。

更详尽的内容参见:

  1. http://zh.wikipedia.org/wiki/Redis
  2. http://redis.io

ServiceStack操作Redis

public static void Main(string[] args)

        {

            var Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口  



            #region insert



            var storeMembers = new List<string> { "aaa", "bbb", "ccc" };

            storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));

            //注:也可直接使用AddRangeToList方法将一组数据装入如:  



            Redis.AddRangeToList("additemtolist", storeMembers);

            #endregion



            #region get



            var members = Redis.GetAllItemsFromList("additemtolist");

            members.ForEach(s => Console.WriteLine("additemtolist :" + s));

            Console.WriteLine(Redis.GetItemFromList("additemtolist", 2));

            #endregion



            #region delete



            var list = Redis.Lists["additemtolist"];

            list.Clear();//清空  

            list.Remove("two");//移除指定键值  

            list.RemoveAt(2);//移除指定索引位置数据  

            #endregion



            #region object



            Redis.Set("userinfo", new UserInfo() { UserName = "张三", Age = 11 });

            UserInfo userinfo = Redis.Get<UserInfo>("userinfo");

            Console.WriteLine(userinfo.UserName);



            Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);  

            Console.WriteLine(Redis.Get<int>("my_age"));

            #endregion



            var ser = new ObjectSerializer();    //位于namespace ServiceStack.Redis.Support;  



            #region 序列化

            bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName = "龙一", Age = 12 }));

            UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;

            Console.WriteLine(userinfo.UserName);

            #endregion



            //也支持列表  

            Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));



            #region 负载均衡



            PooledRedisClientManager prcm = CreateManager(new string[] { "10.0.4.210:6379" }, new string[] { "10.0.4.210:6379" });

            List<UserInfo> userinfoList = new List<UserInfo>();

            userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });

            userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });





            // 从池中获取一个链接:  



            using (IRedisClient Redis2 = prcm.GetClient())

            {

                Redis2.Set("userinfolist", userinfoList);

                List<UserInfo> userList = Redis2.Get<List<UserInfo>>("userinfolist");

            }



            #endregion



            Console.ReadLine();

        }



        public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)

        {

            //支持读写分离,均衡负载  

            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig

            {

                MaxWritePoolSize = 5,//“写”链接池链接数  

                MaxReadPoolSize = 5,//“写”链接池链接数  

                AutoStart = true,

            });

        }

 

 

你可能感兴趣的:(service)