Re-encapsulation of Redis access based on Servicestack.redis

Source: Internet
Author: User
Tags failover

    1. Overview

RedisOpLib.dll is based on the re-encapsulation of redis operations on Servicestack.redis, which is simple to use after referencing this class

2. Composition

A. RedisOpLib.dll encapsulated Master File

B. redis.config the default configuration file, you can also arbitrarily add such as redisa.config,redisb.config, this configuration file structure is as follows:

<configuration>
<AutoStart>YES</AutoStart>
<IdleTimeOutSecs>0</IdleTimeOutSecs>
<MaxReadPoolSize>1000</MaxReadPoolSize>
<MaxWritePoolSize>1000</MaxWritePoolSize>
<PoolTimeout>0</PoolTimeout>

<!--read address with multiple addresses separated by commas (,)
<ReadonlyHost>192.168.2.238:6380</ReadonlyHost>

<!--read and write addresses, multiple addresses separated by commas (,)

<ReadWriteHost>192.168.2.238:6380</ReadWriteHost>
<SocketReceiveTimeout>0</SocketReceiveTimeout>
<SocketSendTimeout>0</SocketSendTimeout>

<!--for method Tooptimizedresultusingcache, whether Redis easing is enabled (not important, can be specified manually in the program)-
<OptimizedResultUsingCache>True</OptimizedResultUsingCache>

<!--whether to buffer cache entries to String.Empty or null--
<OptimizedResultUsingCacheIncludeNullObject>False</OptimizedResultUsingCacheIncludeNullObject>
<RedisMode>POOL</RedisMode>
<CacheKeys>
<key name= "regionlist" value= "2:0:0"/> <!-- cache type regionlist is valid for 2 hours--
</CacheKeys>
</configuration>

Redismode is defined as follows:

<summary>
Balance,failover can only use readwritehost address
The default is pool, but there is a problem with the Redis address that can cause other program errors if there is a failure
</summary>
Internal enum Redis_mode
{
Pool,//using connection pooling to use Master-slave replication
BALANCE,//Load Balancing mode calculates the hash based on key and then takes the corresponding service according to the hash.
FAILOVER//Failover mode, the address obtained after balance Ping does not pass to try another address
}

3. Program source code

Http://files.cnblogs.com/victor596/RedisOpLib.rar

4. Call

A. Find in Redis first, then return directly, not found then call anonymous function to return data and deposit into Redis at the same time.

 return redisoplib.tooptimizedresultusingcache2<
                 list<clskeyvalue<string, scorecalcparameters>>> (
                Common.getperfpars () , "Perfpars", () => //perfpars is defined in redis.config cache type   

{
Servicestackapp appsetting = new Servicestackapp ();
Appsetting.getsettings ();
return appSetting.PerfConfig.PerfDefineList;
});

B. Direct Access

String rediskey = Common.getrestartcommand (TAG);
string ret = redisoplib.get<string> (Rediskey);

Again such as:

Redisoplib.get<string> ("Redisa", "Yourrediskey")

This code uses the Redisa.config file as its configuration file, not the Redis.config.

You can also specify the cache type When you increase the Redis cache entry

public static bool Set<t> (string key, T obj, string cachekeysetting)//cachekeysetting is the type of cache

This is the case, please treatise the wrong place.

Re-encapsulation of Redis access based on Servicestack.redis

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.