본문 바로가기
Backend/CodeIgniter

[CodeIgniter] 캐싱 드라이버

by pin9___9 2023. 1. 4.
728x90

캐싱 드라이버란?

CodeIgniter에서 기본적으로 제공하는 기능으로 동적 캐싱에 대한 래퍼런스입니다. Caching Driver는 파일 캐시 DB부하를 줄이고 속도를 개선해 줍니다. 파일 기반 캐싱 이외는 특정한 서버 요구 사항이 필요하며, 만약 필요한 요구사항이 만족되지 않으면 오류가 발생합니다.

 

사용법(예제)

  • 캐시 드라이버를 로드하고, 사용하는 드라이버로 APC를 지정합니다. 만약 APC를 사용할 수 없는 경우 파일 기반 캐싱으로 대체합니다.
$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

if ( ! $foo = $this->cache->get('foo'))
{
        echo 'Saving to the cache!<br />';
        $foo = 'foobarbaz!';

        // 5분동안 cache를 저장합니다
        $this->cache->save('foo', $foo, 300);
}

echo $foo;

 

  • 만약 같은 환경에서 여러 애플리케이션을 사용할 때는 캐시 접두어 사용으로 중복을 방지할 수 있습니다.
$this->load->driver('cache',
        array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => 'my_')
);

$this->cache->get('foo'); // my_foo로 가져옴

 

클래스 레퍼런스

class CI_Cache

is_supported ($driver)
인수 $driver (string) – the name of the caching driver
반환값 TRUE if supported, FALSE if not
반환형 bool

  이 함수는 $this->cache->get()를 통해 드라이버에 접근할 때 자동으로 호출됩니다. 그러나, 만약 별도의 드라이버를 사용하는 경우 이 함수를 호출하여 드라이버가 서버 환경에서 지원되는지 확인해야 합니다.

if ($this->cache->apc->is_supported()
{
        if ($data = $this->cache->apc->get('my_cache'))
        {
                // do things.
        }
}

 

 

get ($id)
인수 $id (string) – Cache item name
반환값 Item value or FALSE if not found
반환형 mixed

 

  이 함수는 캐시에서 한 항목을 가져옵니다. 만약 해당 항목이없는 경우, 이 메소드는 FALSE 을 반환합니다.

$foo = $this->cache->get('my_cached_item');

 

 

save($id, $data[, $ttl = 60[, $raw = FALSE]])
인수
  • $id (string) – Cache item name
  • $data (mixed) – the data to save
  • $ttl (int) – Time To Live, in seconds (default 60)
  • $raw (bool) – Whether to store the raw value
반환값 TRUE on success, FALSE on failure
반환형 문자열

  캐시에 한 항목을 저장합니다. 저장에 실패하면 FALSE를 리턴합니다.

$this->cache->save('cache_item_id', 'data_to_cache');

  ✔Note

$raw 파라미터는 increment() 와 decrement()를 사용하기 위해, APC와 Memcache에서만 사용됩니다.

 

 

delete ($id)
인수 $id (string) – name of cached item
반환값 TRUE on success, FALSE on failure
반환형 bool

  캐시에서 지정한 항목을 제거합니다.제거에 실패하면 FALSE를 리턴합니다.

$this->cache->delete('cache_item_id');
728x90

'Backend > CodeIgniter' 카테고리의 다른 글

[Codeigniter] DB에서 원하는 값 가져오기!  (2) 2023.01.20

댓글