zoukankan      html  css  js  c++  java
  • Redis

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using ServiceStack.Redis;
    namespace WebApplication_Redis.RedisDB
    {
        public class RedisHelper
        {
            private RedisHelper()
            { }
            private static RedisClient _RedisClient;
            public static  RedisClient GetRedisClient()
            {
                if (_RedisClient == null)
                {
                    _RedisClient = new RedisClient("127.0.0.1", 6379);
                    _RedisClient.FlushAll();//清空数据库
                }
                return _RedisClient;
            }
        }
    }
    ///*****///
    using ServiceStack.Redis;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using WebApplication_Redis.Models;
    namespace WebApplication_Redis.RedisDB
    {
        public class RedisBLL
        {
            RedisClient client = RedisHelper.GetRedisClient();
            string querykey = "query";
            string addkey = "add";
            string updatekey = "update";
            string deletekey = "delete";
            public List<Student> GetStuList(string opname)
            {
                if (!client.ContainsKey(querykey))
                {
                    var list = new List<Student>();
                    client.Set<List<Student>>(querykey, list);
                }
                return RedisHelper.GetRedisClient().Get<List<Student>>(querykey);
            }
            public void KeepSameWithDB()
            {
                var list = new List<Student>();
                client.Set<List<Student>>("query", list);
            }
            public int Add(Student stu)
            {
                RedisHelper.GetRedisClient().Set<Student>(addkey,stu);
                return 0;
            }
            public int Update(Student stu)
            {
                RedisHelper.GetRedisClient().Set<Student>(updatekey, stu);
                return 0;
            }
            public int Delete(List<int> ids)
            {
                RedisHelper.GetRedisClient().Set<List<int>>(deletekey, ids);
                return 0;
            }
        }
    }
    ///****///
    using ServiceStack.Redis;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using WebApplication_Redis.Models;
    using WebApplication_Redis.RedisDB;
    namespace WebApplication_Redis.Controllers
    {
        public class DefaultController : Controller
        {
            // GET: Default
            public ActionResult Index()
            {
                var list = GetStudents();
                //list = GetStudents();
                return View(list);
            }
            public List<Student> GetStudents()
            {
                //在Redis中存储常用的5种数据类型:String,Hash,List,SetSorted set
                //RedisClient client = new RedisClient("127.0.0.1", 6379);
                //client.FlushAll();//清空数据库
                var client = RedisHelper.GetRedisClient();

                List<Student> stulist = new List<Student>();
                stulist.Add(new Student {  Id=1,  Name="张三", Age=18});
                stulist.Add(new Student { Id = 2, Name = "李四", Age = 19 });
                stulist.Add(new Student { Id = 3, Name = "王五", Age = 20 });
                if (!client.ContainsKey("StudentList"))
                {
                    client.Set<List<Student>>("StudentList", stulist);
                }
                else
                {
                    client.Set<List<Student>>("StudentList", new List<Student>());
                }
               
                return client.Get<List<Student>>("StudentList");
            }
        }
    }
     
    /***/**/
  • 相关阅读:
    查看JVM使用的默认的垃圾收集器
    生产环境mysql的参数设置不一样,好好的程序,又出错
    伤秦姝行
    《道德经》全文——马王堆出土帛书版
    100篇锻炼口才表达能力的绕口令
    《道德经》部分
    40篇英语短文搞定3500个单词
    python浮点数与整数间的转化
    理解微积分
    matlab判断某个变量是否存在
  • 原文地址:https://www.cnblogs.com/GuoLianSheng/p/13275671.html
Copyright © 2011-2022 走看看