DEV Community

Cover image for Using Memcache for Session Storage in Legacy Symfony 1.4/1.5 Projects
Adam
Adam

Posted on

Using Memcache for Session Storage in Legacy Symfony 1.4/1.5 Projects

Introduction

If you're maintaining a legacy Symfony 1.4/1.5 project and need to implement session storage with Memcache, this guide will help you get it up and running properly.

Prerequisites

  • Symfony 1.4/1.5 project
  • Docker environment
  • PHP 7.4 (recommended for legacy Symfony)
  • Memcached server

Step 1: Configure Your PHP Container

First, you'll need to install the Memcache extension in your PHP container:

# Install memcache extension (note: memcache, not memcached)
RUN apt-get update && apt-get install -y \
libmemcached-dev \
&& pecl install memcache-4.0.5.2 \
&& docker-php-ext-enable memcache

Note: We specifically use memcache-4.0.5.2 as it's compatible with PHP 7.4.

Step 3: Verify Your Setup

You can verify your Memcache session storage is working by connecting to your Memcached container and running some diagnostic commands:

`# Connect to your memcached container
docker exec -it your_memcached_container bash

Check general stats

echo "stats" | nc localhost 11211

Check session items

echo "stats items" | nc localhost 11211

View specific slab contents (replace X with slab ID from stats items)

echo "stats cachedump X 100" | nc localhost 11211`

Key Statistics to Watch

When checking your Memcache stats, pay attention to:

  • curr_items: Current number of items stored
  • get_hits/get_misses: Success rate of session retrievals
  • bytes: Memory usage
  • evictions: Should be 0 unless under memory pressure

Common Issues and Solutions

  1. Class Not Found Errors
    If you see Class 'sfMemcacheCache' not found, ensure:
    Memcache extension is properly installed
    Your cache is cleared (php symfony cc)

  2. Connection Issues
    If sessions aren't persisting, verify:
    Memcached host is correctly specified
    Port 11211 is accessible
    Persistent connections are enabled

  3. Performance Optimization
    For better performance:
    Use IGBINARY serializer
    Enable persistent connections
    Set appropriate prefix to avoid collisions
    Use compiled mode

Conclusion

Using Memcache for session storage in legacy Symfony projects can significantly improve performance and scalability. The configuration shown above provides a robust solution that works well with Symfony 1.4/1.5's architecture.

Remember to:

Use the correct Memcache extension version
Configure appropriate session lifetimes
Monitor memory usage
Set meaningful prefixes for multi-app environments

Top comments (0)