2017-10-08 13 views
0

Symfony를 사용하여 내 엔티티를 저장하는 데 Cache Manager (FilesystemCache)을 사용하고 싶습니다.Symfony 캐시에 부분 정보 저장

$find = $this->entityManager->getRepository(User::class) 
     ->getSingleResult([ 
      'whereId' => $id, 
     ]); 

    dump($find); // First dump() 
    $this->cacheManager->set('user-1', $find); 
    dump($this->cacheManager->get('user-1')); // Second dump() 

먼저 덤프 : 나는 이런 짓을

User {#457 ▼ 
    -id: 1 
    -username: "[email protected]" 
    -email: "[email protected]" 
    -plainPassword: null 
    -password: "$2y$13$sFSRu2lyDmrfFZP24vkeQOiaYJ5E2eo/kLlaIITgwTrwZunEBcO1q" 
    -onlineAt: DateTime {#432 ▶} 
    -changePasswordAt: null 
    -roles: array:3 [▶] 
    -registerToken: null 
    -versions: PersistentCollection {#558 ▶} 
    -deletedAt: null 
    -deletedBy: null 
} 

둘째 덤프 :

User {#188 ▼ 
    -id: 1 
    -username: "[email protected]" 
    -email: null 
    -plainPassword: null 
    -password: "$2y$13$sFSRu2lyDmrfFZP24vkeQOiaYJ5E2eo/kLlaIITgwTrwZunEBcO1q" 
    -onlineAt: null 
    -changePasswordAt: null 
    -roles: [] 
    -registerToken: null 
    -versions: null 
    -deletedAt: null 
    -deletedBy: null 
} 

왜 내 두 번째 덤프()가 모든 정보를 가지고하지 않는 이유는 무엇입니까?

답변

1

캐시에 객체를 저장하려고하기 때문에 어떤 속성을 보호해야하는지, 그렇지 않은지를 PHP에 알려주기 위해서는 Serializable이어야합니다. 비교를 위해 FOSUserBundle이 어떻게 동작하는지 볼 수 있습니다.

더 큰 객체 그래프 (객체 및 해당 관계, 예 : versions 속성)를 사용하면 데이터를 직렬화/직렬화 해제 할 때 메모리 문제가 발생할 수 있으므로 캐시에 모든 정보를 저장하는 것은 일반적으로 불가능합니다. 이 Symfony의 경우 UserProvider에는 나머지 데이터를 다시로드하는 refreshUser() 메서드가 있습니다. 따라서 사용자 관리의 일부로 데이터를 캐시하려면이 메서드를 구현하여이 메서드가 호출 될 때마다 나머지 데이터가 반입되는지 확인하는 것이 좋습니다.