• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#操作redis代码汇总

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

马上要用redis来改造现有的o2o项目了,在linux下部署了个redis,顺便研究了下代码操作,分享下代码

using System;
using System.Collections.Generic;
using ServiceStack.Redis;

namespace SysBuild
{
    class Program
    {
        //linux服务器地址
        static private string host = "182.180.50.168";
        //static private string host = "127.0.0.1";
        static private int port = 6379;
        static RedisClient redisClient = new RedisClient(host, port);//redis服务IP和端口
        static void Main(string[] args)
        {
            //创建一个键a,指定值
            redisClient.Set<string>("a", "1");
            //获取键a对应的值
            var a1 = redisClient.Get<string>("a");
            //删除键a
            redisClient.Remove("a");
            //创建一个键a,指定值,并指定10s有效
            redisClient.Set<string>("b", "1", new TimeSpan(0, 0, 60));

            //队列操作之入队
            redisClient.EnqueueItemOnList("list", "haha");
            redisClient.EnqueueItemOnList("list", "haha111111111");
            redisClient.EnqueueItemOnList("list", "haha22");
            redisClient.EnqueueItemOnList("list", "hahset33333333333333333");
            //读取队列深度
            var a = redisClient.GetListCount("list");
            //队列操作之出队
            for (int i = 0; i < a; i++)
            {
                Console.WriteLine(redisClient.DequeueItemFromList("list"));
            }


            redisClient.SetEntryInHash("Hash", "Name", "wujf");
            redisClient.SetEntryInHash("Hash", "Age", "99");
            redisClient.SetEntryInHash("Hash", "Sex", "");
            redisClient.SetEntryInHash("Hash", "Address", "上海市XX号XX室");
            //集合类指定超时的方法
            redisClient.ExpireEntryIn("Hash", new TimeSpan(0, 0, 100));
            var dic = redisClient.GetAllEntriesFromHash("Hash");
            Console.WriteLine("Key-----Value");
            foreach (var keyVal in dic)
            {
                Console.WriteLine(string.Format("{0}-----{1}", keyVal.Key, keyVal.Value));
            }
            List<string> haskKey = redisClient.GetHashKeys("Hash");
            List<string> haskVal = redisClient.GetHashValues("Hash");
            foreach (string key in haskKey)
            {
                Console.WriteLine("HashID--Key:{0}", key);
            }
            foreach (string val in haskVal)
            {
                Console.WriteLine("HashID--val:{0}", val);
            }

            //栈使用,先进后出
            redisClient.PushItemToList("stack", "1");
            redisClient.PushItemToList("stack", "2");
            redisClient.PushItemToList("stack", "3");
            redisClient.PushItemToList("stack", "4");
            int count = redisClient.GetListCount("stack");
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(redisClient.PopItemFromList("stack"));
            }

            //对Set类型进行操作  
            redisClient.AddItemToSet("set", "ddd");
            redisClient.AddItemToSet("set", "ccc");
            redisClient.AddItemToSet("set", "tttt");
            redisClient.AddItemToSet("set", "sssh");
            redisClient.AddItemToSet("set", "hhhh");
            HashSet<string> hashset = redisClient.GetAllItemsFromSet("set");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

            //求并集              
            redisClient.AddItemToSet("set1", "aa");
            redisClient.AddItemToSet("set1", "bb");
            redisClient.AddItemToSet("set2", "bb");
            redisClient.AddItemToSet("set2", "cc");
            hashset = redisClient.GetUnionFromSets(new string[] { "set1", "set2" });
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }
            //求交集  
            hashset = redisClient.GetIntersectFromSets(new string[] { "set1", "set2" });
            //求差集.  
            hashset = redisClient.GetDifferencesFromSet("set1", new string[] { "set2" });

            //Sorted Set类型排序
            redisClient.AddItemToSortedSet("sortList", "1");
            redisClient.AddItemToSortedSet("sortList", "9");
            redisClient.AddItemToSortedSet("sortList", "3");
            redisClient.AddItemToSortedSet("sortList", "8");
            List<string> sortList = redisClient.GetAllItemsFromSortedSet("sortList");
            foreach (string str in sortList)
            {
                Console.WriteLine(str);
            }


            Console.ReadKey();
        }
    }
}

另:运行过程推荐使用Redis客户端看看数据变化(ps:我用的redisclient)

推荐api说明

http://www.cnblogs.com/kissdodog/p/3572084.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#高级编程笔记Day1,2016年8月30日名词定义发布时间:2022-07-13
下一篇:
C#属性(Attribute)用法实例解析发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap