NoSQL implementation in PHP

    Speed, friends, is one of the important components of your project. As long as the resource is visited by 10-100 users a day, then everything is fine - the user is happy and invites his friends, friends of his friends, and so on. The load is growing ...

    For a person who watches photos, reads blogs, listens to music on the Internet, it doesn’t matter what language your site is written in, which DBMS is used and whether you use caching. It is important for him that access to interesting content is as fast as possible.

    But over time, a successful project will always require more productivity due to increased project attendance.

    Consider a bunch of PHP + MySQL + Memcached .

    To show the user an article with comments, you need:
    1. See if there is an article (with comments) in the cache
    2. If not, then make “dear” JOIN text + comments
    3. Put everything in the cache
    4. Share content with user
    The big minus in this algorithm is the muscular JOIN (or any other DBMS).
    The modern noSQL concept has emerged as an answer to normalized data structures.

    Consider the PHP + Memcached bundle in the previous example:
    1. See if there is an article (with comments) in the cache
    2. If not, then put it in the cache
    3. Share content with user
    One point has become "easier . " No JOIN, but where to get the data? Store in files .

    The term “document” appears (for example: an article with comments). To understand more specifically in noSQL technology, I wanted to “come up with a bicycle” as always .

    JIM2 prototype

    Required: Apache + mod_php + php_xcache

    Specify the path where the files will be stored:
    define('STORAGE_PATH', 'F:\\tmp\\jim2');

    Usage Example:

    Recording
    $collection = new Collection();

    $vasya = array
    (
     'name' => 'Vasya',
     'sex' => 'male'
    );

    $masha = array
    (
     'name' => 'Masha',
     'sex' => 'female'
    );

    $jim = array
    (
     'name' => 'Jim',
     'sex' => 'male',
     'type' => 'dog'
    );

    $collection->put($vasya);
    $collection->put($masha);
    $collection->put($jim);

    $collection->createIndex( array ('name+sex', 'sex') );

    reading
    $collection = new Collection();

    $keys = xcache_get('./index/sex/male');

    foreach ($keys as $key)
    {
     print_r( $collection->get($key) );
    }
    Features:
    1. Documents are stored as key / value
    2. Using document properties, you can build indexes (additional keys are created)
    3. Each document has a _rev field indicating the version of the document. This will prevent collisions when locking the file for writing, as document versions are checked
    4. Also, the collection itself stores the version. Made so that you can verify the relevance of caches built on the basis of a collection of documents
    What is not yet, but will be:
    1. Fetching a collection using the API
    2. Pagination
    3. ... any of your suggestions
    PS I did not compare the read speed, since it is obvious that a direct cache access will always work quickly. But the insertion speed (100 elements) pleased: the MySQL + memcached bundle fell behind (0.5417142) from JIM2 (0.4791223)

    Also popular now: