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

Redis 专栏收录该内容
4 篇文章 0 订阅

概述

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

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>
  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

iml6yu

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值