Skip to content

Commit 9b528d9

Browse files
committed
updates components to 0.8
1 parent e9f8924 commit 9b528d9

File tree

21 files changed

+1548
-73
lines changed

21 files changed

+1548
-73
lines changed

.version.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"strategy": "semver",
33
"major": 0,
4-
"minor": 7,
5-
"patch": 3,
4+
"minor": 8,
5+
"patch": 0,
66
"build": 0
77
}

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
"php": "^8.4",
1414
"ext-curl": "*",
1515
"ext-json": "*",
16-
"neuron-php/application": "0.7.*",
17-
"neuron-php/routing": "^0.6.8",
16+
"neuron-php/application": "0.8.*",
17+
"neuron-php/routing": "0.8.*",
1818
"league/commonmark": "^2.6",
19-
"neuron-php/cli": "0.1.*"
19+
"neuron-php/cli": "0.8.*"
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "9.*",
2323
"phpmd/phpmd": "^2.15",
2424
"mikey179/vfsstream": "^1.6"
2525
},
26+
"suggest": {
27+
"ext-redis": "Required for Redis cache storage backend"
28+
},
2629
"autoload": {
2730
"files": [
2831
"src/Bootstrap.php"

examples/config/config.yaml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,49 @@ views:
99

1010
cache:
1111
enabled: true
12-
storage: file
13-
path: cache/views
12+
storage: file # Options: 'file' or 'redis'
13+
path: cache/views # Path for file storage
1414
ttl: 3600
1515
html: true
1616
markdown: true
1717
json: false
1818
xml: false
1919

20+
# Redis configuration (used when storage: redis)
21+
# redis_host: 127.0.0.1
22+
# redis_port: 6379
23+
# redis_database: 0
24+
# redis_prefix: neuron_cache_
25+
# redis_timeout: 2.0
26+
# redis_auth: null
27+
# redis_persistent: false
28+
2029
# Garbage collection settings (optional, defaults shown)
2130
# gc_probability: 0.01 # 1% chance to run GC on cache write
2231
# gc_divisor: 100 # Used with probability for fine-tuning
2332

33+
# Rate limiting configuration
34+
rate_limit:
35+
enabled: false # Enable/disable rate limiting
36+
global: false # Apply to all routes globally
37+
storage: file # Storage backend: file, redis, memory (testing only)
38+
requests: 100 # Maximum requests per window
39+
window: 3600 # Time window in seconds (3600 = 1 hour)
40+
file_path: cache/rate_limits
41+
# Redis configuration (if storage: redis)
42+
# redis_host: 127.0.0.1
43+
# redis_port: 6379
44+
# redis_database: 0
45+
# redis_prefix: rate_limit_
46+
47+
# API rate limiting (higher limits)
48+
api_limit:
49+
enabled: false
50+
storage: file
51+
requests: 1000 # 1000 requests
52+
window: 3600 # Per hour
53+
file_path: cache/api_limits
54+
2455
system:
2556
timezone: US/Eastern
2657
base_path: examples

readme.md

Lines changed: 238 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,65 @@ class NotFoundController extends HttpCodes
529529

530530
### View Caching
531531

532-
The framework includes a sophisticated view caching system:
532+
The framework includes a sophisticated view caching system with multiple storage backends:
533+
534+
#### Storage Backends
535+
536+
1. **File Storage** (Default): Uses the local filesystem for cache storage
537+
2. **Redis Storage**: High-performance in-memory caching with Redis
538+
539+
#### Features
533540

534541
1. **Automatic Cache Key Generation**: Based on controller, view, and data
535542
2. **Selective Caching**: Enable/disable per view type
536543
3. **TTL Support**: Configure expiration times
537544
4. **Garbage Collection**: Automatic cleanup of expired entries
545+
5. **Multiple Storage Backends**: Choose between file or Redis storage
546+
547+
#### Configuration
548+
549+
##### File Storage Configuration
550+
```yaml
551+
cache:
552+
enabled: true
553+
storage: file
554+
path: cache/views
555+
ttl: 3600
556+
views:
557+
html: true
558+
markdown: true
559+
json: false
560+
xml: false
561+
```
562+
563+
##### Redis Storage Configuration
564+
```yaml
565+
cache:
566+
enabled: true
567+
storage: redis # Use Redis instead of file storage
568+
ttl: 3600
569+
# Redis configuration (flat structure for env variable compatibility)
570+
redis_host: 127.0.0.1
571+
redis_port: 6379
572+
redis_database: 0
573+
redis_prefix: neuron_cache_
574+
redis_timeout: 2.0
575+
redis_auth: null # Optional: Redis password
576+
redis_persistent: false # Optional: Use persistent connections
577+
# View-specific cache settings
578+
html: true
579+
markdown: true
580+
json: false
581+
xml: false
582+
```
583+
584+
This flat structure ensures compatibility with environment variables:
585+
- `CACHE_STORAGE=redis`
586+
- `CACHE_REDIS_HOST=127.0.0.1`
587+
- `CACHE_REDIS_PORT=6379`
588+
- etc.
589+
590+
#### Programmatic Usage
538591

539592
```php
540593
// Cache is automatically used when enabled
@@ -546,17 +599,40 @@ $html = $this->renderHtml(
546599
);
547600
```
548601

549-
### Manual Cache Management
602+
#### Manual Cache Management
550603

551604
```php
552-
// Clear all expired cache entries
605+
// Clear all expired cache entries (file storage only)
553606
$removed = ClearExpiredCache($app);
554607
echo "Removed $removed expired cache entries";
555608
556609
// Clear all cache
557610
$app->getViewCache()->clear();
558611
```
559612

613+
#### Using CacheStorageFactory
614+
615+
```php
616+
use Neuron\Mvc\Cache\Storage\CacheStorageFactory;
617+
618+
// Create storage based on configuration
619+
$storage = CacheStorageFactory::create([
620+
'storage' => 'redis',
621+
'redis_host' => 'localhost',
622+
'redis_port' => 6379,
623+
'redis_database' => 0,
624+
'redis_prefix' => 'neuron_cache_'
625+
]);
626+
627+
// Auto-detect best available storage
628+
$storage = CacheStorageFactory::createAutoDetect();
629+
630+
// Check storage availability
631+
if (CacheStorageFactory::isAvailable('redis')) {
632+
echo "Redis cache is available";
633+
}
634+
```
635+
560636
You can also manage cache using the CLI commands. See [CLI Commands](#cli-commands) section for details.
561637

562638
### Custom View Implementations
@@ -674,6 +750,165 @@ Recommendations:
674750
Run: neuron mvc:cache:clear --expired
675751
```
676752
753+
## Rate Limiting
754+
755+
The MVC component includes integrated rate limiting support through the routing component. Rate limiting helps protect your application from abuse and ensures fair resource usage.
756+
757+
### Configuration
758+
759+
Rate limiting is configured in your `config.yaml` file using two categories:
760+
761+
#### Standard Rate Limiting
762+
```yaml
763+
rate_limit:
764+
enabled: false # Enable/disable rate limiting
765+
global: false # Apply to all routes globally
766+
storage: file # Storage backend: file, redis, memory (testing only)
767+
requests: 100 # Maximum requests per window
768+
window: 3600 # Time window in seconds (1 hour)
769+
file_path: cache/rate_limits
770+
# Redis configuration (if storage: redis)
771+
# redis_host: 127.0.0.1
772+
# redis_port: 6379
773+
```
774+
775+
#### API Rate Limiting (Higher Limits)
776+
```yaml
777+
api_limit:
778+
enabled: false
779+
storage: file
780+
requests: 1000 # 1000 requests per hour
781+
window: 3600
782+
file_path: cache/api_limits
783+
```
784+
785+
### Environment Variables
786+
787+
Configuration maps to environment variables using the `{category}_{name}` pattern:
788+
- `RATE_LIMIT_ENABLED=true`
789+
- `RATE_LIMIT_STORAGE=redis`
790+
- `RATE_LIMIT_REQUESTS=100`
791+
- `API_LIMIT_ENABLED=true`
792+
- `API_LIMIT_REQUESTS=1000`
793+
794+
### Usage in Routes
795+
796+
#### Global Application
797+
Set `global: true` in configuration to apply rate limiting to all routes:
798+
```yaml
799+
rate_limit:
800+
enabled: true
801+
global: true
802+
requests: 100
803+
window: 3600
804+
```
805+
806+
#### Per-Route Application
807+
Apply rate limiting to specific routes using the `filter` parameter in `routes.yaml`:
808+
809+
```yaml
810+
routes:
811+
# Public page - no rate limiting
812+
- name: home
813+
method: GET
814+
route: /
815+
controller: HomeController@index
816+
817+
# Standard protected route with rate limiting
818+
- name: user_profile
819+
method: GET
820+
route: /user/profile
821+
controller: UserController@profile
822+
filter: rate_limit # Apply rate_limit (100/hour)
823+
824+
# API endpoint with higher limits
825+
- name: api_users
826+
method: GET
827+
route: /api/users
828+
controller: ApiController@users
829+
filter: api_limit # Apply api_limit (1000/hour)
830+
```
831+
832+
### Storage Backends
833+
834+
#### File Storage (Default)
835+
Best for single-server deployments:
836+
```yaml
837+
rate_limit:
838+
storage: file
839+
file_path: cache/rate_limits # Directory for rate limit files
840+
```
841+
842+
#### Redis Storage (Recommended for Production)
843+
Best for distributed systems and high traffic:
844+
```yaml
845+
rate_limit:
846+
storage: redis
847+
redis_host: 127.0.0.1
848+
redis_port: 6379
849+
redis_database: 0
850+
redis_prefix: rate_limit_
851+
redis_auth: password # Optional
852+
redis_persistent: true # Use persistent connections
853+
```
854+
855+
#### Memory Storage (Testing Only)
856+
For unit tests and development. Data is lost when PHP process ends:
857+
```yaml
858+
rate_limit:
859+
storage: memory
860+
```
861+
862+
### Rate Limit Headers
863+
864+
When rate limiting is active, the following headers are included in responses:
865+
- `X-RateLimit-Limit`: Maximum requests allowed
866+
- `X-RateLimit-Remaining`: Requests remaining in current window
867+
- `X-RateLimit-Reset`: Unix timestamp when limit resets
868+
869+
When limit is exceeded (HTTP 429):
870+
- `Retry-After`: Seconds until retry is allowed
871+
872+
### Example Implementation
873+
874+
1. Enable rate limiting in `config.yaml`:
875+
```yaml
876+
rate_limit:
877+
enabled: true
878+
global: false
879+
storage: redis
880+
requests: 100
881+
window: 3600
882+
redis_host: 127.0.0.1
883+
884+
api_limit:
885+
enabled: true
886+
storage: redis
887+
requests: 1000
888+
window: 3600
889+
redis_host: 127.0.0.1
890+
```
891+
892+
2. Apply to routes in `routes.yaml`:
893+
```yaml
894+
routes:
895+
- name: login
896+
method: POST
897+
route: /auth/login
898+
controller: AuthController@login
899+
filter: rate_limit # Strict limit for login attempts
900+
901+
- name: api_data
902+
method: GET
903+
route: /api/data
904+
controller: ApiController@getData
905+
filter: api_limit # Higher limit for API access
906+
```
907+
908+
### Customization
909+
910+
For advanced use cases, you can extend the rate limiting system by creating custom filters in your application. The rate limiting system automatically detects if the routing component version supports it and gracefully degrades if not available.
911+
677912
### Route Management Commands
678913

679914
#### mvc:routes:list

0 commit comments

Comments
 (0)