个人未完成的网站

[StackExchange]Redis 的几种类型的操作(string,hash,lists,set,sorted set)

概述

通过网页进行测试,页面有录入和读取按钮,如下图:
这里写图片描述

String

   /// <summary>
        /// 插入字符串
        /// </summary>
        /// <returns></returns>
        public string Inserttest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.StringSet("test", Request.Params["value"]).ToString();

        }
          /// <summary>
        /// 插入hash
        /// </summary>
        /// <returns></returns>
        public string Inserthtest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.HashSet("htest", Guid.NewGuid().ToString(), Request.Params["value"]).ToString();
        }

Hash

   /// <summary>
        /// 插入list
        /// </summary>
        /// <returns></returns>
        public string Insertltest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.ListLeftPush("ltest", Request.Params["value"]).ToString();

        }
          /// <summary>
        /// 获取hash
        /// </summary>
        /// <returns></returns>
        public string Gethtest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            var result = db.HashGetAll("htest");
            return string.Join("|", result.Select(item => item.Name + ":" + item.Value).ToList<string>());
        }

Lists

  /// <summary>
        /// 插入list
        /// </summary>
        /// <returns></returns>
        public string Insertltest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.ListLeftPush("ltest", Request.Params["value"]).ToString();

        }
          /// <summary>
        /// 获取list
        /// </summary>
        /// <returns></returns>
        public string Getltest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            System.Text.StringBuilder result = new System.Text.StringBuilder("本次读取将清空List:");
            for (var i = db.ListLength("ltest"); i > 0; i--)
            {
                result.Append(db.ListLeftPop("ltest") + ",");
            }
            return result.ToString();
        }

Set

  /// <summary>
        /// 插入set
        /// </summary>
        /// <returns></returns>
        public string Insertstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.SetAdd("stest", Request.Params["value"]).ToString();
        }
           /// <summary>
        /// 获取set
        /// </summary>
        /// <returns></returns>
        public string Getstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            var result = db.SetMembers("stest");
            return string.Join(",", result);
        }

Sorted Set

/// <summary>
        /// 插入排序set
        /// </summary>
        /// <returns></returns>
        public string Insertsstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.SortedSetAdd("sstest", Request.Params["value"], (new Random()).Next(0, 100)).ToString();
        }
          /// <summary>
        /// 获取排序set
        /// </summary>
        /// <returns></returns>
        public string Getsstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            var result = db.SortedSetScan("sstest");
            return string.Join("|", result.Select(item => item.Element + ":" + item.Score).ToList<string>());
        }

总结

整体来说本demo只对几个类型做了简单的插入与读取操作,在操作过程中发现了Lists是采用的入队列出队列的方式进行操作了,和C#中的List不同,不能遍历数据而不改变数据集(目前了解的是这样,不知是否有其他方式遍历)。

运行结果

这里写图片描述

完整代码

controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StackExchange.Redis;

namespace RedisSession.Controllers
{
    public class IndexController : Controller
    {
        //
        // GET: /Index/
        public static ConnectionMultiplexer connect = ConnectionMultiplexer.Connect(("192.168.224.134:6379,abortConnect=false,ssl=false,password=123"));

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 插入字符串
        /// </summary>
        /// <returns></returns>
        public string Inserttest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.StringSet("test", Request.Params["value"]).ToString();

        }
        /// <summary>
        /// 插入hash
        /// </summary>
        /// <returns></returns>
        public string Inserthtest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.HashSet("htest", Guid.NewGuid().ToString(), Request.Params["value"]).ToString();
        }
        /// <summary>
        /// 插入list
        /// </summary>
        /// <returns></returns>
        public string Insertltest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.ListLeftPush("ltest", Request.Params["value"]).ToString();

        }
        /// <summary>
        /// 插入set
        /// </summary>
        /// <returns></returns>
        public string Insertstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.SetAdd("stest", Request.Params["value"]).ToString();
        }
        /// <summary>
        /// 插入排序set
        /// </summary>
        /// <returns></returns>
        public string Insertsstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.SortedSetAdd("sstest", Request.Params["value"], (new Random()).Next(0, 100)).ToString();
        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <returns></returns>
        public string Gettest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            return db.StringGet("test").ToString();

        }
        /// <summary>
        /// 获取hash
        /// </summary>
        /// <returns></returns>
        public string Gethtest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            var result = db.HashGetAll("htest");
            return string.Join("|", result.Select(item => item.Name + ":" + item.Value).ToList<string>());
        }
        /// <summary>
        /// 获取list
        /// </summary>
        /// <returns></returns>
        public string Getltest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            System.Text.StringBuilder result = new System.Text.StringBuilder("本次读取将清空List:");
            for (var i = db.ListLength("ltest"); i > 0; i--)
            {
                result.Append(db.ListLeftPop("ltest") + ",");
            }
            return result.ToString();
        }
        /// <summary>
        /// 获取set
        /// </summary>
        /// <returns></returns>
        public string Getstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            var result = db.SetMembers("stest");
            return string.Join(",", result);
        }
        /// <summary>
        /// 获取排序set
        /// </summary>
        /// <returns></returns>
        public string Getsstest()
        {
            //操作数据库0
            var db = connect.GetDatabase(0);
            var result = db.SortedSetScan("sstest");
            return string.Join("|", result.Select(item => item.Element + ":" + item.Score).ToList<string>());
        }
    }
}

html

<style>
    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }

        table.gridtable th {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #dedede;
        }

        table.gridtable td {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #ffffff;
        }
</style>
<script src="../../Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
    function insert(method) {
        $.post("/Index/Insert" + method, { value: $('#' + method).val() }, function (data) {
            alert(data);
        }, 'text');
    }
    function getdata(method) {
        $.get("/Index/Get" + method, function (data) {
            $('#r' + method).text(data);
        }, 'text');
    }
</script>
<h2>Redis 的几种类型的操作(string,hash,lists,set,sort set)</h2>
<table class="gridtable">
    <thead>
        <tr>
            <th>string</th>
            <th>hash</th>
            <th>lists</th>
            <th>set</th>
            <th>sort set</th>
        </tr>
    </thead>
    <tr>
        <td>Key:test</td>
        <td>Key:htest</td>
        <td>Key:ltest</td>
        <td>Key:stest</td>
        <td>Key:sstest</td>
    </tr>
    <tr>
        <td>值:<input id="test" style="width: 80px;" /><input type="button" value="入库" onclick="insert('test')" /></td>
        <td>值:<input id="htest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('htest')" /></td>
        <td>值:<input id="ltest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('ltest')" /></td>
        <td>值:<input id="stest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('stest')" /></td>
        <td>值:<input id="sstest" style="width: 80px;" /><input type="button" value="入库" onclick="insert('sstest')" /></td>
    </tr>
    <tr>
        <td>结果:<span id="rtest" style="width: 80px;"></span><input type="button" value="读取" onclick="getdata('test')" /></td>
        <td>结果:<span id="rhtest" style="width: 80px;"></span><input type="button" value="读取" onclick="getdata('htest')" /></td>
        <td>结果:<span id="rltest" style="width: 80px;"></span><input type="button" value="读取" onclick="getdata('ltest')" /></td>
        <td>结果:<span id="rstest" style="width: 80px;"></span><input type="button" value="读取" onclick="getdata('stest')" /></td>
        <td>结果:<span id="rsstest" style="width: 80px;"></span><input type="button" value="读取" onclick="getdata('sstest')" /></td>
    </tr>
</table>

posted on 2017-08-29 17:47  我是小虫  阅读(259)  评论(0编辑  收藏  举报

导航