C # Redis Combat (iii)

Source: Internet
Author: User
Tags connectionstrings

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <!--
  3. For more information about how to configure an ASP. NET application, go to
  4. http://go.microsoft.com/fwlink/? linkid=169433
  5. -->
  6. <configuration>
  7. <configsections>
  8. <!--For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/? linkid=237468 --
  9. <section name="entityframework" type=" System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=5.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 " requirepermission=" false " />
  10. <section name="Redisconfig" type="Redisdemo.redisconfiginfo, Redisdemo"/>
  11. </configsections>
  12. <redisconfig writeserverlist="127.0.0.1:6379" readserverlist="127.0.0.1:6379 " Maxwritepoolsize="
  13. maxreadpoolsize= " autostart=" true " localcachetime=" " recordelog=" false " >
  14. </redisconfig>
  15. <connectionStrings>
  16. <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 " />
  17. </connectionStrings>
  18. </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
  1. Public static Redisconfiginfo GetConfig ()
  2. {
  3. Redisconfiginfo section = (redisconfiginfo) configurationmanager.getsection ("Redisconfig");
  4. return section;
  5. }
  6. public static Redisconfiginfo getconfig (string sectionname)
  7. {
  8. Redisconfiginfo section = (redisconfiginfo) configurationmanager.getsection ("Redisconfig");
  9. if (section = = null)
  10. throw New Configurationerrorsexception ("section" + sectionname + "are not found.");
  11. return section;
  12. }
Redis Management Class Code: [CSharp]View Plaincopy
  1. <summary>
  2. /// Redis configuration file Information
  3. // </summary>
  4. private static Redisconfiginfo Redisconfiginfo = Redisconfiginfo.getconfig ();
  5. private static Pooledredisclientmanager PRCM;
  6. // <summary>
  7. /// static construction method, initialize the link Pool Management Object
  8. // </summary>
  9. static Redismanager ()
  10. {
  11. Createmanager ();
  12. }
  13. // <summary>
  14. /// Create a linked Pool management object
  15. // </summary>
  16. private static void Createmanager ()
  17. {
  18. string[] writeserverlist = splitstring (Redisconfiginfo.writeserverlist, ",");
  19. string[] readserverlist = splitstring (Redisconfiginfo.readserverlist, ",");
  20. PRCM = New Pooledredisclientmanager (Readserverlist, Writeserverlist,
  21. New Redisclientmanagerconfig
  22. {
  23. Maxwritepoolsize = Redisconfiginfo.maxwritepoolsize,
  24. Maxreadpoolsize = Redisconfiginfo.maxreadpoolsize,
  25. AutoStart = Redisconfiginfo.autostart,
  26. });
  27. }
  28. private static string[] Splitstring (string strsource, string split)
  29. {
  30. return Strsource.split (Split.  ToArray ());
  31. }
  32. // <summary>
  33. /// Client Cache Action Object
  34. // </summary>
  35. public static Iredisclient getclient ()
  36. {
  37. if (PRCM = = null)
  38. Createmanager ();
  39. return PRCM.  Getclient ();
  40. }
If you want to reprint, please specify the source, this series of blog sample program

C # Redis Combat (iii)

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.