Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
An Introduction to Scaling Symfony
1. Scaling Symfony
Whip your Website into Shape
Brent Shaffer
@bshaffer
CentreSource Interactive Agency
www.brentertainment.com
1
2. Can Symfony Scale?
Dan Grossman: Symfony is used for Delicious and Yahoo!
Bookmarks, which “[proves] it’s enterprise-ready and able to scale up
to millions of active users”
Delicious says: “our new platform has been stable and is
meeting our performance goals. In the days of the old Delicious, we
had to put a lot of effort into just keeping the lights on and
struggling to keep up with “growth. With our new infrastructure,
these problems are largely gone and our pagers have never been so
quiet. But more importantly the Delicious service is now faster and
more reliable, which was a key goal of this project. In fact, I’m very
happy to say that Delicious has experienced zero downtime since the
day after launch”
*http://www.dangrossman.info/2008/08/03/delicious-proof-that-symfony-is-a-scalable-framework/
2
3. It’s All About The Cache
Cache == Time
Performance time, processing time, etc.
Time == Money
Money == Cash
Cache == Cash?*
*Yes, this is a bad joke. Please disregard it.
3
4. Where Do I Spend my Cache?
Types of Cache Cache Drivers
Memcache
Byte-Code Cache
APC
Doctrine Query Cache
FileCache*
Doctrine Result Cache
XCache
Page Cache
E Accelerator*
Partial Cache
DB Cache (Doctrine Only)
Array Cache (Memory Storage)
*Not available in Doctrine
4
5. Query Caching
Query Lifecycle:
“The query cache has no disadvantages...
1.Init new DQL query
always use query caching in your
2.Parse DQL query
production environment”
3.Build database specific SQL query
- Doctrine Website
4.Execute the SQL query
5.Build the result set
6.Return the result set
There IS a disadvantage: Query Cache must be cleared upon updates to your DB.
- Less-than Doctrine2: Restart Apache, remove database if applicable, etc.
- Doctrine 2: use “clear cache” task
Query Caching can be specified at the manager, connection, and query levels
$managerOrConn->setAttribute(Doctrine::ATTR_QUERY_CACHE, $q = Doctrine_Query::create()
new Doctrine_Cache_Apc()); ->useQueryCache(new Doctrine_Cache_Memcache());
Always use Prepared Statements (placeholders for dynamic content)
Right! WRONG!
$query->where('a.id = ?', $id); $query->where('a.id = '. $id);
5
6. Result Caching
Query Lifecycle:
1.Init new DQL query
2.Parse DQL query
3.Build database specific SQL query
4.Execute the SQL query
5.Build the result set
6.Return the result set
Use Doctrine Attributes to set result caching at the manager, connection, or
query level
$managerOrConn->setAttribute( $q = Doctrine_Query::create()
Doctrine::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc()); ->useResultCache(new Doctrine_Cache_Memcache());
$managerOrConn->setAttribute(
Doctrine::ATTR_RESULT_CACHE_LIFESPAN,
new Doctrine_Cache_Apc());
Activate Result Cache on a query level
$query->useResultCache(); // Designate query to use result cache
$query->expireResultCache(); // Force result cache expiration
$query->expireQueryCache(); // Force query cache expiration
6
7. View Cache
Action Cache
# /path/to/project/apps/myapp/modules/mymodule/config/cache.yml
list:
enabled: true
with_layout: false # Default Value
lifetime: 86400 # Default Value
Partial/Component/Slot Cache
# /path/to/project/apps/myapp/modules/mymodule/config/cache.yml
_partial_or_component:
enabled: true
with_layout: false # Default Value
lifetime: 86400 # Default Value
Template Fragment Cache
<?php use_helper('Cache') ?>
7
8. Action Caching
Action Cache Lifecycle
An incoming request with GET parameters or submitted with the POST,
PUT, or DELETE method will never be cached by symfony, regardless of
the configuration.
8
9. Action Caching
Layout Cache Lifecycle
This time, the whole response object is stored in cache.This means
that the cache with layout is much faster than the cache without it.
9
10. Doctrine View Cache
(http://symplist.net/plugins/sfDoctrineViewCachePlugin)
Utilizes Doctrine Routes and a Doctrine Behavior to clear the
view cache for specified actions of the same class
# /path/to/project/config/doctrine/schema.yml
BlogPost:
actAs:
sfViewCache:
global: true # clear cache globally
clear_routes: frontend # clear sfDoctrineRoutes for this object class
on_delete: false # trigger on event (also: on_create and on_update)
Can specify if cache is cleared on Insert, Update, and Delete
Limitations:
1. No application control for individual routes 4. All configurations take place in your model’s
SCHEMA?!
2. Redeclaration of routing rules (name to module/action/
params) 5. Great concept, lacks polish
3. No official release
10
11. Partial/Component/Slot
Caching
Slots are part of the template, and caching an action will also
store the value of the slots defined in this action's template. So
the cache works natively for slots.
Partial/component caching is only useful when utilized outside of
action caching or with partials/components within a layout.
Caching for global partials and components (the ones located in
the application templates/ directory) takes place in the application
cache.yml.
11
12. Fragment Caching
Use Cache Helper
<!-- Template Code (not cached) -->
<h1>Welcome, <?php echo $sf_user->getGuardUser() ?>!</h1>
<!-- Cached code -->
<?php if (!cache('products', 43200)): ?>
<?php foreach ($products as $product): ?>
<li><?php echo link_to($product, '@product?slug='.$product['slug']) ?></li>
<?php endforeach; ?>
<?php cache_save() ?>
<?php endif; ?>
Fragment caching is useful only if contained in an uncached view
12
13. Tips and Tricks
sfTesterViewCache
public function isCached($boolean, $with_layout = false)
sfSuperCache
Writes view cache to web directory to allow apache to serve up pages
without starting symfony
npAssetsOptimizer - minification
http://symplist.net/plugins/npAssetsOptimizerPlugin
13
14. Tips and Tricks
Lazy Route Serialization
all:
routing:
class: sfPatternRouting
param:
generate_shortest_url: true
extra_parameters_as_query_string: true
lazy_routes_deserialize: true
Clear individual templates / environments:
$ php symfony cc --type=template --env=prod --app=frontend
(config, i18n, routing, module and template types)
System Tuning
Mysql - http://www.day32.com/MySQL/
14