Sina App Engine (SAE) Introductory tutorial (3)-kvdb use

Source: Internet
Author: User
Tags cloud computing platforms

Brief introduction

Because of the shortcomings of traditional relational database in distributed environment, the concept of NoSQL has gradually become the focus of the industry in recent years, and more and more technical staff are accustomed to using NoSQL database for daily development, and SAE has also carried out the research and development of NoSQL in order to deal with this new demand.

KV DB is a distributed Key-value data storage service developed by SAE to support massive key-value storage on public cloud computing platforms. KV DB supports a large storage capacity, support for each user 100G of storage space, can support 1,000,000,000 records, users can use KV DB to store simple data, such as friend relationship.

What can I do with kvdb?
    • Deposit key value (for example, a counter used as a single article)
    • Storing arrays (such as accessing user information)
    • Save the file (this is also possible, although not very recommended, below will give an example)
    • Other (do what you want)
Kvdb Using Useful resources
    • API Documentation
    • SAE Documentation
Kvdb Use Instances

The above describes the use of Kvdb can be, the following will be introduced from the above several aspects of the specific examples of the use of SAE Kvdb. Before you use it, you need to start applying the KVDB to the SAE admin panel. As shown in, click Kvdb in the "Service list"

After opening, we can use the KVDB service provided by SAE, the following simple script test under the KVDB service, by the way Kvdb the first application, direct user key value access. The code for the test is as follows:

<? PHP $kv New saekv (); // initializing the Kvclient object $ret $kv, init (); Var_dump ($ret);? >

Access http://lazydemo.sinaapp.com/saekv/test_kv.php can see that the output is:

BOOL (true)

This means that we have successfully used the KVDB service. Below we will use KVDB as our counter, the principle of the counter is the page is refreshed, the counter plus 1. The code is as follows:

<?PHP$kv=Newsaekv ();//initializing the Kvclient object$ret=$kv-init ();if($kv->get (' count ') = =NULL){        $kv->set (' Count ', ' 1 ');//the value of the initialization counter is 1, ensuring that only one initial value is set at initialization time}$now=$kv->get (' count ');//Remove the current count value$setvalue=$now+1;//value of the counter plus 1$kv->set (' Count ',$setvalue);//Write to CounterEcho"This page has been visted".$setvalue;?>

Visit http://lazydemo.sinaapp.com/saekv/count_kv.php to see the effect ~

The following describes the use of Kvdb to access an array, such as access to a user's information.

The code is as follows:

<?PHP$kv=Newsaekv ();//initializing the Kvclient object$ret=$kv-init ();$myinfo=Array(' name ' = ' lazy ', ' age ' = ' + ', ' email ' = ' [email protected] ');$kv->set (' lazy ',$myinfo);//write an array of my information into the KVDB//and then take it out$myinfo _inkvdb=$kv->get (' lazy ');Var_dump($myinfo _inkvdb);?>

Visit http://lazydemo.sinaapp.com/saekv/array_kv.php

Array string string string ("[Email protected]"}

As you can see, the message is a complete array, which is more convenient than MySQL access-this is just a simple example, of course, there are many applications.

The above has introduced the two applications of KVDB, the following describes the use of KVDB access files, speaking of files, will certainly think of binary, kvdb value of the maximum length: 4M (by default, compression is enabled), that is, we have to access the file size can not exceed 4M, Is it possible that more than 4M of files will not be accessible? The answer is no, one of my colleague Pagnee wrote an example shows that KVDB can be scattered when accessing the file and then the output when the file splicing output, but as the introductory tutorial no longer described, interested can find the relevant information on their own to see.
The following example is the use of KVDB to access the image, the specific code is as follows:

<?PHP$kv=Newsaekv ();//initializing the Kvclient object$ret=$kv-init ();$f=NewSaefetchurl ();//fetch usage will be described in a later article$img _data=$f->fetch (' http://ss7.sinaimg.cn/bmiddle/488efcbbt7b5c4ae51ca6&690 ');//Retrieve binary data$kv->set (' Image ',$img _data);//Enter Picture$img _data_inkvdb=$kv->get (' Image ');Header("Content-type:image/jpg");Echo($img _data_inkvdb);?>

Original Image: http://ss7.sinaimg.cn/bmiddle/488efcbbt7b5c4ae51ca6&690
Image of the output after access: http://lazydemo.sinaapp.com/saekv/storimage_kv.php

This part of the code package download

Http://lazydemo.sinaapp.com/saekv/saekv.zip

Sina App Engine (SAE) Introductory tutorial (3)-kvdb use

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.