PHP code example of imiphp / imi-shared-memory

1. Go to this page and download the library: Download imiphp/imi-shared-memory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imiphp / imi-shared-memory example snippets


[
    'components'    =>  [
        // 引入本组件
        'SharedMemory'    =>  'Imi\SharedMemory',
    ],
    'pools'    =>    [
        // 客户端连接池
        'sharedMemory'  =>  [
            'async' =>  [
                'pool'    =>    [
                    'class'        =>    \Imi\SharedMemory\Pool\ClientPool::class,
                    'config'    =>    [
                        'maxResources'    =>    100,
                        'minResources'    =>    0,
                    ],
                ],
                'resource'  =>  [
                    // 以下为可选配置

                    // 存储器类型,注意和下面的类名不同
                    // 'storeTypes'    =>  [
                    //     \Yurun\Swoole\SharedMemory\Client\Store\KV::class,
                    //     \Yurun\Swoole\SharedMemory\Client\Store\Stack::class,
                    //     \Yurun\Swoole\SharedMemory\Client\Store\Queue::class,
                    //     \Yurun\Swoole\SharedMemory\Client\Store\PriorityQueue::class,
                    //      'name'  =>  'XXXClass',
                    // ],

                    // unix socket 文件名,默认会自动放 runtime 目录中
                    // 'socketFile'    =>  '',
                ],
            ],
        ]
    ],
    // 以下为可选配置
    // 'swooleSharedMemory'    =>  [
        // unix socket 文件名,默认会自动放 runtime 目录中
        // 'socketFile'    =>  '',

        // 存储器类型,注意和上面的类名不同
        // 'storeTypes'    =>  [
        //     \Yurun\Swoole\SharedMemory\Store\KV::class,
        //     \Yurun\Swoole\SharedMemory\Store\Stack::class,
        //     \Yurun\Swoole\SharedMemory\Store\Queue::class,
        //     \Yurun\Swoole\SharedMemory\Store\PriorityQueue::class,
        //      'name'  =>  'XXXClass',
        // ],

        // 默认连接池名
        // 'defaultPool'   =>  'sharedMemory'
    // ],
]

// 方法一
SharedMemory::use('KV', function(\Yurun\Swoole\SharedMemory\Client\Store\KV $kv){
    $kv->set('a', 1);
});

// 方法二
$kv = SharedMemory::getInstance()->getObject('KV');
$kv->set('a', 1);