18/12/2015 - DOCTRINE, MEMCACHED, SYMFONY
Bu örnek doctrine/cache
paketi üzerine kuruludur. Öncelikle php.ini ve OS içinde memcached'in aktif hale getirilmesi gereklidir. Örneğimiz memcached'i kullanıp nasıl performansı arttıracağımızı gösterecek. Daha fazla bilgi için Doctrine Caching chapter, memcached kopya kağıdı ve phpMemcachedAdmin sayfalarını ziyaret edebilirsiniz.
Her test arasında memcached'den yeni bir sonuç almak isterseniz, aşağıdaki komutları kullanabilirsiniz.
# List running processes
top
# Kill given process
kill -9 process_id
# Start memcached deamon
memcached -d
# Connect to memcached
telnet 127.0.01 11211
Aşağıdaki bölümleri isterseniz tek tek, isterseniz de hep birlikte kullanabilirsiniz.
doctrine:
orm:
metadata_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
query_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
result_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
Eğer isterseniz entity manager içinde de aşağıdaki gibi ayarlama yapabilirsiniz.
doctrine:
...
orm:
...
entity_managers:
em_one:
metadata_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
query_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
result_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
...
em_two:
metadata_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
query_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
result_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
...
Diğer bir yazımızda da gösterdiğimiz gibi, eğer memcached'i app/config/services.yml dosyası içinde servis olarak tanımlarsanız, aşağıdaki konfigürasyonu kullanabilirsiniz. Bu şekilde memcached servisini, uygulamanız içinde farklı işlemler içinde kullanabilirsiniz.
doctrine:
orm:
metadata_cache_driver:
type: service
id: football.doctrine.cache.memcached # Defined in app/config/services.yml
query_cache_driver:
type: service
id: football.doctrine.cache.memcached # Defined in app/config/services.yml
result_cache_driver:
type: service
id: football.doctrine.cache.memcached # Defined in app/config/services.yml
public function findAll()
{
$qb = $this->createQueryBuilder('l')
->select('l, t, p')
->innerJoin('l.team', 't')
->innerJoin('t.player', 'p')
->orderBy('l.name', 'ASC')
->addOrderBy('t.name', 'ASC')
->addOrderBy('p.name', 'ASC')
->getQuery();
$qb = $qb->getResult();
return $qb;
}
Memcached aktivitilerini gözlemlemek isterseniz, tarayıcı üzerinden phpMemcachedAdmin kullanabilirsiniz.
Mac:football inanzzz$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
# No memory items found
stats items
END
Millisecond MB
3487 40.5 (*)
665 28.8
669 28.8
679 28.8
668 28.8
Bu işlem için QueryBuilder içinde manual olarak herhangi bir işlem yapmanıza gerek yok.
Mac:football inanzzz$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
# 3 memory items found
stats items
STAT items:13:number 1
STAT items:13:age 30
STAT items:13:evicted 0
STAT items:13:evicted_nonzero 0
STAT items:13:evicted_time 0
STAT items:13:outofmemory 0
STAT items:13:tailrepairs 0
STAT items:13:reclaimed 0
STAT items:13:expired_unfetched 0
STAT items:13:evicted_unfetched 0
STAT items:13:crawler_reclaimed 0
STAT items:13:crawler_items_checked 0
STAT items:13:lrutail_reflocked 0
STAT items:14:number 1
STAT items:14:age 30
STAT items:14:evicted 0
STAT items:14:evicted_nonzero 0
STAT items:14:evicted_time 0
STAT items:14:outofmemory 0
STAT items:14:tailrepairs 0
STAT items:14:reclaimed 0
STAT items:14:expired_unfetched 0
STAT items:14:evicted_unfetched 0
STAT items:14:crawler_reclaimed 0
STAT items:14:crawler_items_checked 0
STAT items:14:lrutail_reflocked 0
STAT items:15:number 1
STAT items:15:age 30
STAT items:15:evicted 0
STAT items:15:evicted_nonzero 0
STAT items:15:evicted_time 0
STAT items:15:outofmemory 0
STAT items:15:tailrepairs 0
STAT items:15:reclaimed 0
STAT items:15:expired_unfetched 0
STAT items:15:evicted_unfetched 0
STAT items:15:crawler_reclaimed 0
STAT items:15:crawler_items_checked 0
STAT items:15:lrutail_reflocked 0
END
# Team entity metadata
stats cachedump 13 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[Application\BackendBundle\Entity\Team$CLASSMETADATA][1] [1036 b; 1450466352 s]
END
# League entity metadata
stats cachedump 14 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[Application\BackendBundle\Entity\League$CLASSMETADATA][1] [1650 b; 1450466352 s]
END
# Player entity metadata
stats cachedump 15 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[Application\BackendBundle\Entity\Player$CLASSMETADATA][1] [1898 b; 1450466352 s]
END
Millisecond MB
3019 40.2 (*)
700 28.5
690 28.5
676 28.5
666 28.5
Aşağıdaki kodu QueryBuilder'e eklemeniz gerekli.
...
->getQuery()
->useQueryCache(true);
Mac:football inanzzz$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
# 1 memory items found
stats items
STAT items:13:number 1
STAT items:13:age 19
STAT items:13:evicted 0
STAT items:13:evicted_nonzero 0
STAT items:13:evicted_time 0
STAT items:13:outofmemory 0
STAT items:13:tailrepairs 0
STAT items:13:reclaimed 0
STAT items:13:expired_unfetched 0
STAT items:13:evicted_unfetched 0
STAT items:13:crawler_reclaimed 0
STAT items:13:crawler_items_checked 0
STAT items:13:lrutail_reflocked 0
END
# Query cache related item
stats cachedump 13 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[07aa386f36aa0c503519f5baf6076bda][1] [1014 b; 1450467303 s]
END
Millisecond MB
2206 40.2 (*)
687 26.5
654 26.5
674 26.5
657 26.5
Aşağıdaki kodu QueryBuilder'e eklemeniz gerekli.
...
->getQuery()
->useResultCache(true, 60, 'my_unique_cache_id');
Mac:football inanzzz$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
# 1 memory items found
stats items
STAT items:29:number 1
STAT items:29:age 23
STAT items:29:evicted 0
STAT items:29:evicted_nonzero 0
STAT items:29:evicted_time 0
STAT items:29:outofmemory 0
STAT items:29:tailrepairs 0
STAT items:29:reclaimed 0
STAT items:29:expired_unfetched 0
STAT items:29:evicted_unfetched 0
STAT items:29:crawler_reclaimed 0
STAT items:29:crawler_items_checked 0
STAT items:29:lrutail_reflocked 0
END
# Result cache related item
stats cachedump 29 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[my_unique_cache_id][1] [44017 b; 1450468525 s]
END
Millisecond MB
3291 41.0 (*)
646 29.0
647 29.0
681 29.0
656 29.0
Mac:football inanzzz$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
# 4 memory items found
stats items
STAT items:13:number 2
STAT items:13:age 29
STAT items:13:evicted 0
STAT items:13:evicted_nonzero 0
STAT items:13:evicted_time 0
STAT items:13:outofmemory 0
STAT items:13:tailrepairs 0
STAT items:13:reclaimed 0
STAT items:13:expired_unfetched 0
STAT items:13:evicted_unfetched 0
STAT items:13:crawler_reclaimed 0
STAT items:13:crawler_items_checked 0
STAT items:13:lrutail_reflocked 0
STAT items:14:number 1
STAT items:14:age 29
STAT items:14:evicted 0
STAT items:14:evicted_nonzero 0
STAT items:14:evicted_time 0
STAT items:14:outofmemory 0
STAT items:14:tailrepairs 0
STAT items:14:reclaimed 0
STAT items:14:expired_unfetched 0
STAT items:14:evicted_unfetched 0
STAT items:14:crawler_reclaimed 0
STAT items:14:crawler_items_checked 0
STAT items:14:lrutail_reflocked 0
STAT items:15:number 1
STAT items:15:age 29
STAT items:15:evicted 0
STAT items:15:evicted_nonzero 0
STAT items:15:evicted_time 0
STAT items:15:outofmemory 0
STAT items:15:tailrepairs 0
STAT items:15:reclaimed 0
STAT items:15:expired_unfetched 0
STAT items:15:evicted_unfetched 0
STAT items:15:crawler_reclaimed 0
STAT items:15:crawler_items_checked 0
STAT items:15:lrutail_reflocked 0
STAT items:29:number 1
STAT items:29:age 28
STAT items:29:evicted 0
STAT items:29:evicted_nonzero 0
STAT items:29:evicted_time 0
STAT items:29:outofmemory 0
STAT items:29:tailrepairs 0
STAT items:29:reclaimed 0
STAT items:29:expired_unfetched 0
STAT items:29:evicted_unfetched 0
STAT items:29:crawler_reclaimed 0
STAT items:29:crawler_items_checked 0
STAT items:29:lrutail_reflocked 0
END
# Query cache related data and Team entity metadata under same item
stats cachedump 13 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[07aa386f36aa0c503519f5baf6076bda][1] [1014 b; 1450468063 s]
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[Application\BackendBundle\Entity\Team$CLASSMETADATA][1] [1036 b; 1450468063 s]
END
# League entity metadata
stats cachedump 14 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[Application\BackendBundle\Entity\League$CLASSMETADATA][1] [1650 b; 1450468063 s]
END
# Player entity metadata
stats cachedump 15 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[Application\BackendBundle\Entity\Player$CLASSMETADATA][1] [1898 b; 1450468063 s]
END
# Result cache related item
stats cachedump 29 100
ITEM sf2orm_default_f2fbb5259b59469e8f50fbd0bfaef6747e168f1e5a445e5cb5812f652c835bf9[my_unique_cache_id][1] [44017 b; 1450468743 s]
END
Millisecond MB
2476 41.2 (*)
928 26.8
641 26.8
628 26.8
637 26.8
618 26.8
645 26.8