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");
            }
        }
    }
     
    /***/**/
  • 相关阅读:
    ASP.NET MVC 几种 Filter 的执行过程源码解析
    C#中的线程二(BeginInvoke和Invoke)
    C#中的线程一(委托中的异步)
    C#比较dynamic和Dictionary性能
    C#微信公众平台开发—高级群发接口
    js 关闭浏览器
    切图神器 --- Assistor
    切图 -- cutterman
    mac上用teamviewer远程windows输入问题
    A quick introduction to HTML
  • 原文地址:https://www.cnblogs.com/GuoLianSheng/p/13275671.html
Copyright © 2011-2022 走看看