Redis简单介绍以及数据类型存储

        因为我们在大型互联网项目其中。用户訪问量比較大,比較多。会产生并发问题,对于此。我们该怎样解决呢。Redis横空出世,首先,我们来简单的认识一下Redis。具体介绍例如以下所看到的:

         Redis是一个开源的。使用C语言编写。面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能。持久存储,适应高并发的应用场景。Redis纯粹为应用而产生,她是一个高性能的Key-Value数据库,而且操作了多种语言的API性能測试将诶过表示SET操作每秒钟可达110000次。GET操作每秒81000次。当然不同的server配置性能不同,redis眼下提供五种数据类型,string(字符串),list(链表),hash(哈希),set(集合)及zset(sorted set)有序集合。

说到Redis,小伙伴们可能会想到Memcached,小编给小伙伴们推荐一篇文章。文中具体的描写叙述了Redis和Memcached,有兴趣的小伙伴能够点击相关链接,进行查看哦`(*∩_∩*)′

         在前面的介绍中。我们知道,redis眼下提供五种数据类型。string(字符串)、list(链表)、hash(哈希)、set(集合)、zset(sorted set)有序集合,那么这些是怎样进行存储的呢?以下小编来具体的介绍一下。

         第一步,我们须要新建一个空白项目,例如以下图所看到的:

         
         第二步,加入一个控制台应用程序。例如以下图所看到的:

         
         第三步,有三个dll文件,redis为c#开放的API,我们就是通过她来操作redis,为此,我们须要引用dll文件。点击浏览,例如以下图所看到的:

         

         第四步,找到须要引用的dll文件。例如以下图所看到的:

         

         接着,我们就来看怎样对数据类型进行存储,首先我们来看String类型。String类型是最经常使用的一种数据类型,普通的key/value存储都能够归为此类,一个key相应一个value,string类型二进制的。Redis的string能够包括不论什么数据,比方jpg或者序列化的对象,我们来看具体的代码该怎样编写。代码例如以下所看到的:

         

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串类型
            var client = new RedisClient("local", 6379); //首先new一个客户端
            client.Set<string>("name", "laowang");       // 存储字符串类型
            string userName = client.Get<string>("name");  //通过get取值
            Console.WriteLine(userName);
            Console.ReadKey();

         }
    }
}

        接着,我们来看哈希表的存储,hash是一个string类型的field和value的映射表。hash特别适合存储对象,相对于讲对象的每一个字段存成单个string类型。一个对象存储在hash类型中会占用更少的内存。而且能够更方便的存取整个对象。

作为一个key  value存在,非常多开发人员自然的使用set/get方式来使用Redis。实际上这并非最优化的用法,尤其在未启用VM情况下,Redis全部数据须要放入内存,节约内存尤其重要,我们来看具体的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串类型
            var client = new RedisClient("local", 6379); //首先new一个客户端
            client.Set<string>("name", "laowang");       // 存储字符串类型
            string userName = client.Get<string>("name");  //通过get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存储结果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //获取全部的key
            client.GetHashValues("userinfoId"); //获取全部的值

          }
    }
}
        我们再来看list类型。list是一个链表结构的,主要功能是push。pop获取一个范围的左右的值等,操作中key理解为链表名称。Redis的list类型事实上就是一个每一个字元素都是string类型的双向链表。我们能够通过push,pop操作从链表的头部或者尾部加入删除元素,这样list既能够作为栈。也能够作为队列。Redis list的实现为一个双向链表。既能够支持反向查找和遍历。更方便操作,只是带来了部分额外的内存开销,Redis内部的非常多实现。包括发送缓冲队列等也都是用的这个数据结构,代码例如以下所看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串类型
            var client = new RedisClient("local", 6379); //首先new一个客户端
            client.Set<string>("name", "laowang");       // 存储字符串类型
            string userName = client.Get<string>("name");  //通过get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存储结果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //获取全部的key
            client.GetHashValues("userinfoId"); //获取全部的值

            //队列使用
            client.EnqueueItemOnList("name1", "laowang");//入队
            client.EnqueueItemOnList("name1", "laoma");//入队
            int length = client.GetListCount("nama1");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name1")); //出队
            }

            //栈的使用
            client.PushItemToList("name2", "laowang");//入栈
            client.PushItemToList("name2", "laoma");
            int lentgh = client.GetListCount("name2");
            {
                Console.WriteLine(client.PopItemFromList("name2"));//出栈
            }

        }
    }
}
        Set类型
        她是string类型的无序集合。set是通过hash table实现的。加入、删除和查找,对集合我们能够取并集、交集、差集。对Set类型进行操作。例如以下所看到的:

        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串类型
            var client = new RedisClient("local", 6379); //首先new一个客户端
            client.Set<string>("name", "laowang");       // 存储字符串类型
            string userName = client.Get<string>("name");  //通过get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存储结果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //获取全部的key
            client.GetHashValues("userinfoId"); //获取全部的值

            //队列使用
            client.EnqueueItemOnList("name1", "laowang");//入队
            client.EnqueueItemOnList("name1", "laoma");//入队
            int length = client.GetListCount("nama1");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name1")); //出队
            }

            //栈的使用
            client.PushItemToList("name2", "laowang");//入栈
            client.PushItemToList("name2", "laoma");
            int lentgh = client.GetListCount("name2");
            {
                Console.WriteLine(client.PopItemFromList("name2"));//出栈
            }

            //对Set类型进行操作
            client.AddItemToSet("a3", "ddd");
            client.AddItemToSet("a3", "ccc");
            client.AddItemToSet("a3", "ttt");
            client.AddItemToSet("a3", "sss");
            client.AddItemToSet("a3", "hhh");
            System.Collections.Generic.HashSet<string> hashset = client.GetAllItemsFromSet("a3");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

        }
    }
}
       Sorted Set类型
       Sorted set是set的一个升级版本号。她是在set的基础撒花姑娘添加了一个顺序的属性。这一个属性在加入改动。元素的时候能够指定,每次指定后。zset(表示有序集合)会自己主动又一次按新的值调整顺序,能够理解为有序列的表,一列存value。一列存顺序。操作中key理解为zset的名字。

Redis sorted set的使用场景与set相似。差别是set不是自己主动有序的,而sorted set能够通过用户额外提供一个优先级(scord)的參数来为成员排序,而且是插入有序的,即自己主动排序。当你须要一个有序的而且不反复的集合列表。那么能够选择sorted set数据结构。代码例如以下所看到的:

       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;

namespace RedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串类型
            var client = new RedisClient("local", 6379); //首先new一个客户端
            client.Set<string>("name", "laowang");       // 存储字符串类型
            string userName = client.Get<string>("name");  //通过get取值
            Console.WriteLine(userName);
            Console.ReadKey();

            //哈希存储结果
            client.SetEntryInHash("userinfoId", "name", "zhangsan");
            client.GetHashKeys("userinfoId"); //获取全部的key
            client.GetHashValues("userinfoId"); //获取全部的值

            //队列使用
            client.EnqueueItemOnList("name1", "laowang");//入队
            client.EnqueueItemOnList("name1", "laoma");//入队
            int length = client.GetListCount("nama1");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name1")); //出队
            }

            //栈的使用
            client.PushItemToList("name2", "laowang");//入栈
            client.PushItemToList("name2", "laoma");
            int lentgh = client.GetListCount("name2");
            {
                Console.WriteLine(client.PopItemFromList("name2"));//出栈
            }

            //对Set类型进行操作
            client.AddItemToSet("a3", "ddd");
            client.AddItemToSet("a3", "ccc");
            client.AddItemToSet("a3", "ttt");
            client.AddItemToSet("a3", "sss");
            client.AddItemToSet("a3", "hhh");
            System.Collections.Generic.HashSet<string> hashset = client.GetAllItemsFromSet("a3");
            foreach (string str in hashset)
            {
                Console.WriteLine(str);
            }

            //Sorted Set类型
            client.AddItemToSortedSet("a5", "ffff");
            client.AddItemToSortedSet("a5", "bbbb");
            client.AddItemToSortedSet("a5", "gggg");
            client.AddItemToSortedSet("a5", "cccc");
            client.AddItemToSortedSet("a5", "aaaa");
            System.Collections.Generic.List<string> list = client.GetAllItemsFromSortedSet("a5");
            foreach (string str in list)
            {
                Console.WriteLine(str);
            }

        }
    }
}
       小编寄语:该博客。小编主要简单的介绍了一下Redis和数据存储的类型,在redis中另一个非常重要的事儿,几乎忘了,文件并发(日志处理),多线程操作同一个文件时会出现并发问题,解决的一个办法就是给文件加锁(lock),可是这种话,一个线程操作文件时,其它的都得等待,这种话性能非常差,另外一个解决方式。就是先将数据放在队列中,然后开启一个线程,负责从队列中取出数据,再写到文件里。

对于这块的内容小编还不是非常理解。还请各位小伙伴多多不吝赐教哦`(*∩_∩*)′!

原文地址:https://www.cnblogs.com/clnchanpin/p/6820701.html