라이브러리

[PHP] SyncSharedMemory::first - 개체가 명명된 공유 메모리의 시스템 전체에서 첫 번째 인스턴스인지 확인합니다.




SyncSharedMemory::first


SyncSharedMemory::first는 Laravel의 SyncSharedMemory 기능을 사용하여 공유 메모리를 동기화하는 메서드입니다. 공유 메모리는 여러 프로세스 간에 데이터를 공유하기 위한 메커니즘입니다. 이 메서드는 공유 메모리에 저장된 데이터를 읽어와 첫 번째 데이터를 반환합니다.

사용 방법


SyncSharedMemory::first를 사용하기 위해서는 Laravel의 SyncSharedMemory 기능을 사용해야 합니다. Laravel 8.x 이상 버전에서 SyncSharedMemory 기능을 사용할 수 있습니다.

# 1. Laravel 프로젝트 생성


Laravel 프로젝트를 생성하고, SyncSharedMemory 기능을 사용하기 위해 `config/cache.php` 파일을 수정합니다.

#hostingforum.kr
php

// config/cache.php



'cache' => [

    // ...

    'store' => env('CACHE_DRIVER', 'file'),

    'stores' => [

        'file' => [

            // ...

        ],

        'sync' => [

            'driver' => 'sync',

            'ttl' => 60,

        ],

    ],

],



# 2. SyncSharedMemory 기능 활성화


`config/app.php` 파일을 수정하여 SyncSharedMemory 기능을 활성화합니다.

#hostingforum.kr
php

// config/app.php



'providers' => [

    // ...

    IlluminateSupportServiceProvider::class,

    IlluminateCacheSyncStoreServiceProvider::class,

],



# 3. SyncSharedMemory::first 사용


SyncSharedMemory::first를 사용하기 위해 `app/Services` 디렉토리에 `SharedMemoryService.php` 파일을 생성합니다.

#hostingforum.kr
php

// app/Services/SharedMemoryService.php



namespace AppServices;



use IlluminateSupportFacadesCache;



class SharedMemoryService

{

    public function getFirstValue()

    {

        return Cache::get('shared_memory');

    }

}



`app/Http/Controllers` 디렉토리에 `SharedMemoryController.php` 파일을 생성합니다.

#hostingforum.kr
php

// app/Http/Controllers/SharedMemoryController.php



namespace AppHttpControllers;



use AppServicesSharedMemoryService;



class SharedMemoryController extends Controller

{

    public function index()

    {

        $sharedMemoryService = new SharedMemoryService();

        $value = $sharedMemoryService->getFirstValue();



        return response()->json(['value' => $value]);

    }

}



# 4. 예제


`php artisan serve` 명령어를 실행하여 Laravel 개발 서버를 시작합니다. `http://localhost:8000/shared-memory` URL을 방문하여 첫 번째 공유 메모리 값을 확인합니다.

예제 코드


#hostingforum.kr
php

// app/Http/Controllers/SharedMemoryController.php



namespace AppHttpControllers;



use AppServicesSharedMemoryService;

use IlluminateHttpRequest;



class SharedMemoryController extends Controller

{

    public function index()

    {

        $sharedMemoryService = new SharedMemoryService();

        $value = $sharedMemoryService->getFirstValue();



        return response()->json(['value' => $value]);

    }



    public function store(Request $request)

    {

        $sharedMemoryService = new SharedMemoryService();

        $sharedMemoryService->setFirstValue($request->input('value'));



        return response()->json(['message' => '공유 메모리 값이 저장되었습니다.']);

    }

}



#hostingforum.kr
php

// app/Services/SharedMemoryService.php



namespace AppServices;



use IlluminateSupportFacadesCache;



class SharedMemoryService

{

    public function getFirstValue()

    {

        return Cache::get('shared_memory');

    }



    public function setFirstValue($value)

    {

        Cache::put('shared_memory', $value);

    }

}



참고


* Laravel 공식 문서: [Sync Shared Memory](https://laravel.com/docs/9.x/cache#sync-shared-memory)
* Laravel 공식 문서: [Cache Store](https://laravel.com/docs/9.x/cache#cache-store)
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 89 페이지

검색

게시물 검색