Sets a key expiry in seconds and reports whether the operation succeeded.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Sets a key expiry in seconds and reports whether the operation succeeded.
Syntax and Return Value
Redis::expire(...)
The return value and exceptional cases depend on the call described here: TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.
Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.
Example 1: Basic Use
Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.
<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->set('guide:expire:1','draft');
echo $redis->expire('guide:expire:1',60+3) ? "expiring" : "absent";
Example 2: A Second Scenario
Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.
<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->set('guide:expire:2','review');
echo $redis->expire('guide:expire:2',60+4) ? "expiring" : "absent";
Example 3: Practical Checks
Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.
<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->set('guide:expire:3','published');
echo $redis->expire('guide:expire:3',60+5) ? "expiring" : "absent";
Important Behavior and Edge Cases
TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| Redis::expire | Sets a key expiry in seconds and reports whether the operation succeeded. |
| Related API / pattern | ttl reports remaining lifetime; setex stores a value and TTL together. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired. |
| Using a related API interchangeably | ttl reports remaining lifetime; setex stores a value and TTL together. |
Key Takeaways
- Sets a key expiry in seconds and reports whether the operation succeeded.
- TTL is reset when you explicitly set expiry; nonexistent keys cannot be expired.
- ttl reports remaining lifetime; setex stores a value and TTL together.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.