High-Performance File Cache key-value storage-Redis, key-valueredis

Source: Internet
Author: User
Tags redis server

High-Performance File Cache key-value storage-Redis, key-valueredis
1. High-Performance File Cache key-value storage-Memcached 2. ASP. NET HttpRuntime. Cache Usage Summary

Note: The three blog posts can be read in combination for easy understanding and use. If you want to learn more, please refer to the blog posts in the article.

1. Preface

A. Redis is an open-source, advanced key-value (key/value Pair) storage solution that is perfect for building high-performance, scalable Web applications.

B. Comparison between Redis and Memcached

B .1 The Redis database is completely in the memory, and the disk is used only for persistence.

Compared with many key-value pairs, apsaradb for redis has more data types. apsaradb for Redis provides five data types: strings, map, list, sets, and sorted sets.

B .3 Redis can copy data to any number of slave servers

C. Advantages of Redis

C.1 Redis performs fast response

C.2 supports a wide range of data types

C.3 atomicity ensures that if two client colleagues access the Redis server, the updated value will be obtained.

C.4 multi-function utility, Redis is a multi-utility tool that can be used in multiple applications, such as cache, message, queue, any temporary data, and applications.

D. Github: https://github.com/kencery/Common/tree/master/KenceryCommonMethod/%E7%BC%93%E5%AD%98

E. Redis learning: http://www.redis.io/

2. Code display
1 // source file header information: 2 // <copyright file = "RedisHelper. cs "> 3 // Copyright (c) 2014-2034 Kencery. all rights reserved. 4 // personal blog: http://www.cnblogs.com/hanyinglong 5 // created by: kencery 6 // Creation Time: 7 // </copyright> 8 9 using System; 10 using System. collections. generic; 11 using System. configuration; 12 using ServiceStack. redis; 13 14 namespace KenceryCommonMethod 15 {16 /// <summary> 17 // Redis cache read setting encapsulation 18 /// <auther> 19 /// <name> Kencery </name> 20 /// <date> </date> 21 /// </auther> 22 /// </summary> 23 public static class RedisHelper 24 {25 /// <summary> 26 // create a Redis connection pool management object (add ServiceStack. interfaces. dll, ServiceStack. redis. dll) 27 /// </summary> 28 // <param name = "readWriteHosts"> write-only server </param> 29 // <param name = "readOnlyHosts"> Read-Only server </param> 30 // <returns> </returns> 31 private static PooledRedisClientManager CreateRedisManager (IEnumerable <string> readWriteHosts, 32 IEnumerable <string> readOnlyHosts) 33 {34 // supports read/write splitting and Server Load balancer 35 Return new PooledRedisClientManager (readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 36 {37 MaxWritePoolSize = 5, // number of "write" link pools 38 MaxReadPoolSize = 5, // number of "read" link pools 39 AutoStart = true, 40}); 41} 42 43 // <summary> 44 // call the CreateRedisManager method to create a connection pool management object, configure the Redis server address in the configuration file (create read-only, write only the connection pool) 45 // <add key = "RedisHosts" value = "127.0.0.1: 6379 "/> 46 // </summary> 47 private static readonly Poole DRedisClientManager Prcm = CreateRedisManager (48 ConfigurationManager. appSettings ["Hosts"]. split (",". toCharArray (), StringSplitOptions. removeEmptyEntries), 49 ConfigurationManager. appSettings ["Hosts"]. split (",". toCharArray (), StringSplitOptions. removeEmptyEntries); 50 51 // <summary> 52 // Add data to the cache using RedisHelper. set (key, value (value to be stored); 53 // </summary> 54 // <typeparam name = "T"> cache type </typeparam> 55 // <param name = "key"> key </param> 56 // <param name = "val"> value </param> 57 public static void Set <t> (string key, T val) 58 {59 using (IRedisClient rdc = Prcm. getClient () 60 {61 rdc. set <T> (key, val); 62} 63} 64 65 // <summary> 66 // read the data in the cache. use var result = RedisHelper. get <T> (key ); 67 /// </summary> 68 // <typeparam name = "T"> return read data </typeparam> 69 // <param name = "key"> key </param> 70 // <return S> </returns> 71 public static T Get <T> (string key) where T: class 72 {73 using (IRedisClient rdc = Prcm. getReadOnlyClient () 74 {75 return rdc. get <T> (key); 76} 77} 78 79 // <summary> 80 // Delete the cached data using RedisHelper. remove (key); 81 // </summary> 82 // <param name = "key"> key </param> 83 public static void Remove (string key) 84 {85 using (IRedisClient rdc = Prcm. getClient () 86 {87 rdc. remove (ke Y); 88} 89} 90 91 // <summary> 92 // whether the cache contains the queried key data, use var isTrue = RedisHelper. containsKey (key); 93 /// </summary> 94 /// <param name = "key"> key </param> 95 /// <returns> if it contains, returns true, otherwise false </returns> 96 public static bool ContainsKey (string key) 97 {98 using (IRedisClient rdc = Prcm. getReadOnlyClient () 99 {100 return rdc. containsKey (key); 101} 102} 103 104 // <summary> 105 // Add an Object to the cache using: R EdisHelper. add (key, value (value to be stored )) 106 /// </summary> 107 // <param name = "key"> key </param> 108 // <param name = "value"> value </ param> 109 public static void Add (string key, object value) 110 {111 using (IRedisClient rdc = Prcm. getClient () 112 {113 if (! Rdc. containsKey (key) 114 {115 rdc. add (key, value, DateTime. now. addMinutes (30); 116} 117 else118 {119 rdc. set (key, value); 120} 121} 122 123 124 // <summary> 125 // refresh the data information in the cache based on the key, use: RedisHelper. refreshCache (key) 126 /// </summary> 127 // <typeparam name = "T"> cache type </typeparam> 128 // <param name = "key"> key </param> 129 public static void RefreshCache <T> (string key) where T: class130 {131 using (IRedisClient rdc = Prcm. getClient () 132 {133 var value = rdc. get <T> (key); 134 rdc. remove (key); 135 rdc. set <T> (key, value); 136} 137} 138 139 // <summary> 140 // read the key-value pairs in the cache based on the key Set information, store data in dictionary format, using RedisHelper. getList (keys ); 141 /// </summary> 142 // <param name = "keys"> key set </param> 143 /// <returns> return dictionary set </returns> 144 public static Dictionary <string, string> GetList (List <string> keys) 145 {146 using (IRedisClient rdc = Prcm. getReadOnlyClient () 147 {148 return rdc. getValuesMap <string> (keys); 149} 150} 151 152 // <summary> 153 // Add the dictionary set to the cache using RedisHelper. set (values ); 154 /// </summary> 155 // <param name = "values"> Dictionary Set Information </param> 156 public static void Set (Dictionary <string, string> values) 157 {158 using (IRedisClient rdc = Prcm. getReadOnlyClient () 159 {160 rdc. setAll (values); 161} 162} 163 164} 165}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.