Using redis to build URL-based shortening services

Source: Internet
Author: User

The English version of this article was first published in codeproject: Building a simple URL shorten service with redis

Before reading this article, I suggest you download the source code, read the article, and read the code.

Download the code here: http://www.codeproject.com/KB/NoSQL/819235/MicroUrl.zip

What is redis?

Simply put, redis is similar to memcached and is also a memory database. However, redis can save data to the hard disk. Therefore, redis can be used as a cache and a nosql database.

Make Web Part

First, create an ASP. net mvc project in Visual Studio using no authentication

Create a new class to store URL data.

Expiremode is a custom enumeration.

Then create the Home Page

This is probably the case.

It is worth mentioning thatHtmlheperOfEnumdropdownlistforIn ASP. net MVC 5.2 was added. In previous versions, it was very troublesome to create these items by ourselves. Now, we can use this method to generate them directly and use attribute to specify the display name.

It's easy to get a successful page.

Save data to redis

With the above preparations, we can start to write redis code.

First, installServicestack. redisIf you need a DLL with a strong name, you can install this nuget package:Servicestack. redis. Signed

Then we use the using statement using to create a client and then generateMicrourlitemsTyped Client

Before saving an object to redis, the object requires a primary key. Here we useServicestack. dataannotations. primarykeyThis attribute is marked, and this attribute is eventually like this

If the user does not specify the shortened URL, a random

This is a simple generation algorithm.

Then you can save it.

In the last step, if the user specifies the validity period, we need to set its expire in redis.

Read data from redis

We use the above method to create a typed client, and then find it in it. If it is found, HTTP 302 is returned. After the URL is accessed, if itsExpiremodeYesBylastaccessedTo renew.

Another thing is in this action.RouteAttribute is not involved in work by default. We need to map it

Finally

Before running the website, we need to start redis (which can be downloaded in the http://redis.io/download) first, for windows, we can call this command to start the redis Server

You can also start a monitor.

Start the website and try out our functions.

The custom URL is normal.

This is a randomly generated URL.

After executing these operations, we can see in the monitor which redis commands have been executed

 

The English version of this article was first published in codeproject: Building a simple URL shorten service with redis

Using redis to build URL-based shortening services

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.