Third, the program configuration in C # Redis Combat (ii) we installed the Redis system services, the Redis service is now running. Now we need to let our program correctly read a series of configuration information such as Redis service address, first of all, you need to add the following information in the Web. config file:
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <!--
- For more information about how to configure an ASP. NET application, go to
- http://go.microsoft.com/fwlink/? linkid=169433
- -->
- <configuration>
- <configsections>
- <!--For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/? linkid=237468 --
- <section name="entityframework" type=" System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=5.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 " requirepermission=" false " />
- <section name="Redisconfig" type="Redisdemo.redisconfiginfo, Redisdemo"/>
- </configsections>
- <redisconfig writeserverlist="127.0.0.1:6379" readserverlist="127.0.0.1:6379 " Maxwritepoolsize="
- maxreadpoolsize= " autostart=" true " localcachetime=" " recordelog=" false " >
- </redisconfig>
- <connectionStrings>
- <add name= "defaultconnection" providername="System.Data.SqlClient " connectionstring="Data source= (LocalDb) \v11.0;initial catalog=aspnet-redisdemo-20131125110945;integrated SECURITY=SSPI; attachdbfilename=| Datadirectory|\aspnet-redisdemo-20131125110945.mdf " />
- </connectionStrings>
- </configuration>
With the above information is not enough, also need to use C # code to read and operate, get Redis configuration program is as follows:
[CSharp]View Plaincopy
- Public static Redisconfiginfo GetConfig ()
- {
- Redisconfiginfo section = (redisconfiginfo) configurationmanager.getsection ("Redisconfig");
- return section;
- }
- public static Redisconfiginfo getconfig (string sectionname)
- {
- Redisconfiginfo section = (redisconfiginfo) configurationmanager.getsection ("Redisconfig");
- if (section = = null)
- throw New Configurationerrorsexception ("section" + sectionname + "are not found.");
- return section;
- }
Redis Management Class Code:
[CSharp]View Plaincopy
- <summary>
- /// Redis configuration file Information
- // </summary>
- private static Redisconfiginfo Redisconfiginfo = Redisconfiginfo.getconfig ();
- private static Pooledredisclientmanager PRCM;
- // <summary>
- /// static construction method, initialize the link Pool Management Object
- // </summary>
- static Redismanager ()
- {
- Createmanager ();
- }
- // <summary>
- /// Create a linked Pool management object
- // </summary>
- private static void Createmanager ()
- {
- string[] writeserverlist = splitstring (Redisconfiginfo.writeserverlist, ",");
- string[] readserverlist = splitstring (Redisconfiginfo.readserverlist, ",");
- PRCM = New Pooledredisclientmanager (Readserverlist, Writeserverlist,
- New Redisclientmanagerconfig
- {
- Maxwritepoolsize = Redisconfiginfo.maxwritepoolsize,
- Maxreadpoolsize = Redisconfiginfo.maxreadpoolsize,
- AutoStart = Redisconfiginfo.autostart,
- });
- }
- private static string[] Splitstring (string strsource, string split)
- {
- return Strsource.split (Split. ToArray ());
- }
- // <summary>
- /// Client Cache Action Object
- // </summary>
- public static Iredisclient getclient ()
- {
- if (PRCM = = null)
- Createmanager ();
- return PRCM. Getclient ();
- }
If you want to reprint, please specify the source, this series of blog sample program
C # Redis Combat (iii)