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");
        }
    }
}
 
/***/**/
原文地址:https://www.cnblogs.com/GuoLianSheng/p/13275671.html