• 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");
            }
        }
    }
     
    /***/**/
  • 相关阅读:
    golang以服务方式运行
    nginx重写规则配置
    PHP的 parse_ini_file 解析配置文件
    在Yii2中集成Markdown编辑器
    理解 is_callable
    Composer安装yii2-imagine 压缩,剪切,旋转,水印
    Yii2 基于header 实现接口版本控制
    (1) laravel php artisan list make
    php 阿里云国内短信实例
    php 云片国外短信实例
  • 原文地址:https://www.cnblogs.com/GuoLianSheng/p/13275671.html
一二三 - 开发者的网上家园