High Performance Website Architecture Design cache Chapter (1)-Redis C # Client

Source: Internet
Author: User
Tags gz file redis version

One, what Redis

REmote DIctionary Server, or Redis, is a memcached-like key-value storage System. Compared to memcached, it supports a richer data structure, including string (string), list (linked list), set (set), Zset (sorted set-ordered set), and hash (hash type), and provides a data persistence mechanism, in some scenarios, You can completely use it as a non-relational database. It is a high-performance storage system capable of supporting more than 100k+ per second of read and write frequency. It also supports the publication/subscription of messages, giving you another option when building a high-performance Message Queuing system.

Second, download

Click here to download.

I am downloading the redis-3.0.0-beta5 version here, and Redis supports the cluster starting from 3.0.
Third, installation

The following installations are all steps on the OSX operating system:

1. Find the redis-3.0.0-beta5.tar.gz file you just downloaded and unzip the file.

2. Copy the extracted folder to a directory that you can easily find, and modify the folder name to Redis because the version number is too long.

2. Open the terminal and enter the Redis folder with the following command:

Last Login:fri-21:33:25 on ttys000

zhaoguihuadediannao:~ zhaogh$ CD Applications/dev/redis

Zhaoguihuadediannao:redis zhaogh$

3. Enter the make command at the command prompt and wait a few moments for the installation to complete. If you are prompted not to find the make command, refer to this article.

installed, is not a little excited. Smoke a cigarette, let's go on.

Iv. Use of

1, start the service, first enter the SRC directory, and then execute redis-server.

Zhaoguihuadediannao:redis zhaogh$ CD src

Zhaoguihuadediannao:src zhaogh$./redis-server

You will see:

2343:m 21:42:50.741 # Server started, Redis version 2.9.54

2343:m-21:42:50.741 * The server is now a ready-to-accept connections on port 6379

I'm not mistaken, TMD. 3.0.0, how do i show Redis version 2.9.54? Well, that's not the point, I'm too lazy to pursue it.

6379 is the Redis default port, and in subsequent articles you will know how to modify this default port.

2, the client connection.

You will find that after the fourth step, we can no longer execute other commands, how to do? Because Redis is monopolizing this process, it tells you later if you modify it to run in the background.

Let's open up another terminal for a moment. or enter the SRC directory first:

zhaoguihuadediannao:~ zhaogh$ CD APPLICATIONS/DEV/REDIS/SRC

ZHAOGUIHUADEDIANNAO:SRC zhaogh$

Enter the./REDIS-CLI command:

Zhaoguihuadediannao:src zhaogh$./redis-cli

127.0.0.1:6379>

It's already connected, hahaha.

3. Test several Redis commands:

127.0.0.1:6379> Set testkey001 testkey001

Ok

127.0.0.1:6379> Get testkey001

"Testkey001"

127.0.0.1:6379> Append testkey001 AAA

(integer) 13

127.0.0.1:6379> Get testkey001

"Testkey001aaa"

127.0.0.1:6379>
4. Close the connection and execute the QUIT command

127.0.0.1:6379> quit

ZHAOGUIHUADEDIANNAO:SRC zhaogh$

5, close the service.

Since we have just exited the client, we connect again and execute the shutdown command:

127.0.0.1:6379> shutdown

A Resdis help class is provided below

<summary>
Redis Public Auxiliary Class library
</summary>
public class Redishelper:idisposable
{
Public redisclient Redis = new Redisclient ("127.0.0.1", 6379);
Cache Pool
Pooledredisclientmanager PRCM = new Pooledredisclientmanager ();
Default cache expiration Time unit seconds
public int secondstimeout = 30 * 60;

<summary>
Buffer pool
</summary>
<param name= "Readwritehosts" ></param>
<param name= "Readonlyhosts" ></param>
<returns></returns>
public static Pooledredisclientmanager Createmanager (
String[] readwritehosts, string[] readonlyhosts)
{
return new Pooledredisclientmanager (Readwritehosts, Readonlyhosts,
New Redisclientmanagerconfig
{
Maxwritepoolsize = Readwritehosts.length * 5,
Maxreadpoolsize = Readonlyhosts.length * 5,
AutoStart = True,
});//{redisclientfactory = (iredisclientfactory) RedisCacheClientFactory.Instance.CreateRedisClient ("127.0.0.1", 6379)};
}


<summary>
constructor function
</summary>
<param name= "Openpooledredis" > whether to open the buffer pool </param>
Public Redishelper (BOOL Openpooledredis = false)
{
if (Openpooledredis)
{
PRCM = Createmanager (new string[] {"127.0.0.1:6379"}, new string[] {"127.0.0.1:6379"});
Redis = PRCM. Getclient () as redisclient;
}
}

#region Key/value Storage
<summary>
Setting up the cache
</summary>
<typeparam name= "T" ></typeparam>
<param name= "key" > Cache build </param>
<param name= "T" > Cache value </param>
<param name= "Timeout" > expiry time, Unit seconds,-1: No period, 0: Default Expiration Time </param>
<returns></returns>
public bool Set<t> (string key, T t, int timeout = 0)
{
if (Timeout >= 0)
{
if (Timeout > 0)
{
Secondstimeout = timeout;
}
Redis.expire (key, secondstimeout);
}

Return redis.add<t> (key, T);
}
<summary>
Get
</summary>
<typeparam name= "T" ></typeparam>
<param name= "Key" ></param>
<returns></returns>
Public T get<t> (string key)
{
return redis.get<t> (key);
}
<summary>
Delete
</summary>
<param name= "Key" ></param>
<returns></returns>
public bool Remove (string key)
{
return Redis.remove (key);
}

public bool Add<t> (string key, T T, int timeout)
{
if (Timeout >= 0)
{
if (Timeout > 0)
{
Secondstimeout = timeout;
}
Redis.expire (key, secondstimeout);
}
Return redis.add<t> (key, T);
}
#endregion

#region Linked list operations
<summary>
Add a linked list based on IEnumerable data
</summary>
<typeparam name= "T" ></typeparam>
<param name= "ListId" ></param>
<param name= "Values" ></param>
<param name= "Timeout" ></param>
public void addlist<t> (string listId, ienumerable<t> values, int timeout = 0)
{
Redis.expire (ListId, 60);
iredistypedclient<t> iredisclient = redis.as<t> ();
if (Timeout >= 0)
{
if (Timeout > 0)
{
Secondstimeout = timeout;
}
Redis.expire (ListId, secondstimeout);
}
var redislist = Iredisclient.lists[listid];
Redislist.addrange (values);
Iredisclient.save ();
}

<summary>
Add a single entity to a linked list
</summary>
<typeparam name= "T" ></typeparam>
<param name= "ListId" ></param>
<param name= "Item" ></param>
<param name= "Timeout" ></param>
public void addentitytolist<t> (string listId, T Item, int timeout = 0)
{
iredistypedclient<t> iredisclient = redis.as<t> ();
if (Timeout >= 0)
{
if (Timeout > 0)
{
Secondstimeout = timeout;
}
Redis.expire (ListId, secondstimeout);
}
var redislist = Iredisclient.lists[listid];
Redislist.add (Item);
Iredisclient.save ();
}
<summary>
Get linked List
</summary>
<typeparam name= "T" ></typeparam>
<param name= "ListId" ></param>
<returns></returns>
Public ienumerable<t> getlist<t> (string listId)
{
iredistypedclient<t> iredisclient = redis.as<t> ();
return Iredisclient.lists[listid];
}
<summary>
Delete a single entity in a linked list
</summary>
<typeparam name= "T" ></typeparam>
<param name= "ListId" ></param>
<param name= "T" ></param>
public void removeentityfromlist<t> (string listId, T t)
{
iredistypedclient<t> iredisclient = redis.as<t> ();
var redislist = Iredisclient.lists[listid];
Redislist.removevalue (t);
Iredisclient.save ();
}
<summary>
Delete an eligible entity based on the Lambada expression
</summary>
<typeparam name= "T" ></typeparam>
<param name= "ListId" ></param>
<param name= "func" ></param>
public void removeentityfromlist<t> (string listId, Func<t, bool> Func)
{
using (iredistypedclient<t> iredisclient = redis.as<t> ())
{
var redislist = Iredisclient.lists[listid];
T value = Redislist.where (func). FirstOrDefault ();
Redislist.removevalue (value);
Iredisclient.save ();
}
}
#endregion

<summary>
Freeing resources
</summary>
public void Dispose ()
{
if (Redis! = null)
{
Redis.dispose ();
Redis = null;
}
Gc. Collect ();
}

High Performance Website Architecture Design cache Chapter (1)-Redis C # Client

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.