C#redis Installation and use

Source: Internet
Author: User
Tags redis server

Redis is a widely used Key/value memory database and one of the fastest key-value distributed caches.

Redis Official website: http://redis.io/

Redis Quick Start Tutorial: http://www.yiibai.com/redis/redis_quick_guide.html

Decompression version: Https://github.com/dmajkic/redis/downloads

Installation version: Https://github.com/rgl/redis/downloads

Servicestack.redis:https://github.com/servicestack/servicestack.redis

Reference:

Http://www.cnblogs.com/zhangweizhong/p/4969240.html

http://blog.csdn.net/qiujialongjjj/article/details/16945569

One, Redis installation

Select a version to download, the package includes 32-bit and 64-bit installation tools, or directly using the installation version of the installation, is the same, the use of the installation version, the installation results

A Redis server system service will appear in the system service after the installation is complete.

Redis-server.exe: The server's daemon startup program.

Redis.conf: Configuration file.

Redis-cli.exe: Command line client.

Redis-check-dump.exe: Local Database check

Redis-check-aof.exe: Update log check

Redis-benchmark.exe: Performance testing tool to test the read and write performance of Redis in your system and in your configuration.

Second, Redis configuration

1. Port port number, for example 6379

2. Bind instance binding access address 127.0.0.1

3. Password for requirepass access

4. Maxheap Remember to open this configuration node and the Redis service will not start. For example Maxheap 1024000000

5. Timeout: Request time-out

6. logfile:log File Location

7. Databases: Number of open databases

8. Dbfilename: Data Snapshot file name (only file name, excluding directory)

Third, Redis use

Locate the Redis-cli.exe file in the Redis installation directory, and double-click to start it:

Test OK, this is the end of the Redis installation, followed by the actual use in C #.

Second, Redis combat

1. Build the project and refer to the four class libraries associated with Servicestack.redis.

2. Configuration files

<!--Redis Start-to-<add key="sessionexpireminutes"Value=" the"/> <add key="redis_server_session"Value="127.0.0.1:6379"/> <add key="Redis_max_read_pool"Value="3"/> <add key="Redis_max_write_pool"Value="1"/> <!--Redis end-->

3. Create a common class for Redis operations Rediscachehelper

usingServicestack.redis;usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication{classRediscachehelper {Private Static ReadOnlyPooledredisclientmanager pool =NULL; Private Static ReadOnly string[] redishosts =NULL;  Public Static intRedismaxreadpool =int. Parse (configurationmanager.appsettings["Redis_max_read_pool"]);  Public Static intRedismaxwritepool =int. Parse (configurationmanager.appsettings["Redis_max_write_pool"]); StaticRediscachehelper () {varRedishoststr = configurationmanager.appsettings["redis_server_session"]; if(!string. IsNullOrEmpty (REDISHOSTSTR)) {redishosts= Redishoststr.split (','); if(Redishosts.length >0) {Pool=NewPooledredisclientmanager (redishosts, redishosts,NewRedisclientmanagerconfig () {maxwritepoolsize=Redismaxwritepool, Maxreadpoolsize=Redismaxreadpool, AutoStart=true                        }); }            }        }         Public Static voidAdd<t> (stringkey, T value, DateTime expiry) {            if(Value = =NULL)            {                return; }            if(Expiry <=DateTime.Now) {Remove (key); return; }            Try            {                if(Pool! =NULL)                {                    using(varR =pool. Getclient ()) {if(r! =NULL) {R.sendtimeout= +; R.set (key, value, expiry-DateTime.Now); }                    }                }            }            Catch(Exception ex) {stringmsg =string. Format ("{0}:{1} has an exception! {2}","Cache","Storage", key); }        }         Public Static voidAdd<t> (stringkey, T value, TimeSpan slidingexpiration) {            if(Value = =NULL)            {                return; }            if(Slidingexpiration.totalseconds <=0) {Remove (key); return; }            Try            {                if(Pool! =NULL)                {                    using(varR =pool. Getclient ()) {if(r! =NULL) {R.sendtimeout= +;                        R.set (key, value, slidingexpiration); }                    }                }            }            Catch(Exception ex) {stringmsg =string. Format ("{0}:{1} has an exception! {2}","Cache","Storage", key); }        }         Public StaticT get<t> (stringkey) {            if(string. IsNullOrEmpty (key)) {return default(T); } T obj=default(T); Try            {                if(Pool! =NULL)                {                    using(varR =pool. Getclient ()) {if(r! =NULL) {R.sendtimeout= +; Obj= r.get<t>(key); }                    }                }            }            Catch(Exception ex) {stringmsg =string. Format ("{0}:{1} has an exception! {2}","Cache","Get", key); }            returnobj; }         Public Static voidRemove (stringkey) {            Try            {                if(Pool! =NULL)                {                    using(varR =pool. Getclient ()) {if(r! =NULL) {R.sendtimeout= +;                        R.remove (key); }                    }                }            }            Catch(Exception ex) {stringmsg =string. Format ("{0}:{1} has an exception! {2}","Cache","Delete", key); }        }         Public Static BOOLExists (stringkey) {            Try            {                if(Pool! =NULL)                {                    using(varR =pool. Getclient ()) {if(r! =NULL) {R.sendtimeout= +; returnR.containskey (key); }                    }                }            }            Catch(Exception ex) {stringmsg =string. Format ("{0}:{1} has an exception! {2}","Cache","whether there is", key); }            return false; }    }}
View Code

4. Program Testing

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Redis Write Cache:"); Rediscachehelper.add ("name1","test1", DateTime.Now.AddDays (1)); Console.WriteLine ("Redis Get cache:"); stringstr = rediscachehelper.get<string> ("name1");            Console.WriteLine (str);        Console.readkey (); }    }}
View Code

5. Input Results

Demo download

C#redis Installation and use

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.