Symfony ile kullanıcı sessionunu memcached içinde iki ayrı şekilde tutabilirsiniz. Birincisi php.ini ve config.yml dosyalarında değişiklik yaparak. Diğeri ise, bu örnekte göreceğimiz gibi, bir symony servis yaratıp onu kullanarak.


Mevcut memcached durumu


Şu anda memcached boş.


stats items
END

Parameters


# app/config/parameters.yml

parameters:
memcache_server: localhost
memcache_port: 11211
session_prefix: sess_
session_expire: 86400

Config


# app/config/config.yml

imports:
- { resource: services.yml }

framework:
session:
name: hello
handler_id: session.handler.memcached

Memcached service


# app/config/services.yml

services:
memcached:
class: Memcached
calls:
- [addServer, [%memcache_server%, %memcache_port%]]

session.handler.memcached:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler
arguments: [@memcached, { prefix: %session_prefix%, expiretime: %session_expire% }]

Test


Bu test içinde iki ayrı user kullanacağım.


# Two browser open but no one logged in yet
# Memcached creates two random items (5&6).
> stats items
...
STAT items:5:age 93
...
STAT items:6:age 160
...
END

stats cachedump 5 100
ITEM sess_o2aevqh8843hqf21j0atqd4sm2 [115 b; 1453291464 s]
END
stats cachedump 6 100
ITEM sess_iovf95r2gha5e67fel60oi8dc7 [192 b; 1453291396 s]
END


# Admin user and Test user logged in
# Memcached replaces random items (5&6) with user dedicated ones (11&12).
> stats items
...
STAT items:11:age 3
...
STAT items:12:age 33
...
END

stats cachedump 11 100
ITEM sess_deof8dqij2uollepo9c86d4591 [832 b; 1453291758 s]
END
stats cachedump 12 100
ITEM sess_gqk36m46ds8eugafueqp2e81a6 [966 b; 1453291729 s]
END


# Admin user and Test user visit other pages, do thing etc.
# Memcached still uses same dedicated items (11&12) for owning users.
> stats items
...
STAT items:11:age 182
...
STAT items:12:age 8
...
END

stats cachedump 11 100
ITEM sess_deof8dqij2uollepo9c86d4591 [832 b; 1453291758 s]
END
stats cachedump 12 100
ITEM sess_gqk36m46ds8eugafueqp2e81a6 [966 b; 1453291932 s]
END


# Both of the users logged out
# Memcached reduces two dedicated items (11&12) into one (6) by keeping the items IDs in it.
> stats items
...
STAT items:6:age 21
...
END

stats cachedump 6 100
ITEM sess_deof8dqij2uollepo9c86d4591 [192 b; 1453292171 s]
ITEM sess_gqk36m46ds8eugafueqp2e81a6 [192 b; 1453292153 s]
END


# Admin user and Test user logged in again
# Memcached removes random item (6) and creates user dedicated ones (11&12) with new IDs.
> stats items
...
STAT items:11:age 44
...
STAT items:12:age 4
...
END

stats cachedump 11 100
ITEM sess_1qefj65qc33l6g047l0rpqpdc6 [832 b; 1453292377 s]
END
stats cachedump 12 100
ITEM sess_n4sai4v9r6naemk7do82mkfn04 [966 b; 1453292417 s]
END