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");
            }
        }
    }
     
    /***/**/
  • 相关阅读:
    IntelliJ IDEA 16创建Web项目
    Error running Tomcat8: Address localhost:1099 is already in use 错误解决
    Hibernate的三种状态
    Hibernate 脏检查和刷新缓存机制
    Windows服务器时间不同步问题
    解决Windows内存问题的两个小工具RamMap和VMMap
    实现多线程异步自动上传本地文件到 Amazon S3
    JS判断用户连续输入
    ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤
    bootstrap的popover在trigger设置为hover时不隐藏popover
  • 原文地址:https://www.cnblogs.com/GuoLianSheng/p/13275671.html
Copyright © 2011-2022 走看看