Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Valuestore Laravel Package

spatie/laravel-valuestore

Store and retrieve loose key/value data in a JSON file with a simple API. Supports put/get with defaults, has/all, forget/flush, increment, and helpers like ArrayAccess and Countable—handy for app settings or small persistent state.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Simple Key-Value Storage: Ideal for lightweight, non-relational data storage (e.g., configs, caches, or transient settings) where JSON persistence is sufficient.
  • Laravel Integration: Seamlessly fits into Laravel’s ecosystem, leveraging its service container and filesystem abstractions.
  • Use Cases:
    • Configuration Management: Store app-wide settings (e.g., feature flags, API keys) without database overhead.
    • Caching Layer: Replace Redis/Memcached for non-critical, short-lived data (e.g., user preferences, session-like data).
    • Migration Data: Temporary storage during deployments or data migrations.
  • Limitations:
    • No ACID Compliance: JSON file storage lacks transactions, concurrency control, or atomicity.
    • Scalability: Single-file storage is not distributed; unsuitable for high-read/write workloads or multi-server setups.
    • No Querying: Keys are flat; no support for complex queries or relationships.

Integration Feasibility

  • Low Barrier to Adoption: Minimal setup (composer install + filesystem access). No database migrations or schema changes required.
  • Laravel Compatibility:
    • Works with Laravel’s filesystem (storage/app/valuestore.json by default).
    • Can integrate with Laravel’s caching layer (e.g., cache the JSON file for performance).
    • Supports dependency injection via Laravel’s service container.
  • Customization:
    • Extendable via events (e.g., ValuestoreSaved, ValuestoreFlushed).
    • Custom storage backends possible (e.g., database, S3) by implementing Valuestore interface.

Technical Risk

  • Data Corruption: JSON file operations (e.g., concurrent writes) could corrupt data. Mitigate with:
    • File locking mechanisms (Laravel’s Storage::lock()).
    • Atomic writes (e.g., write to temp file + rename).
  • Performance:
    • Reads: Fast for small datasets (JSON parsing overhead).
    • Writes: Slower for large datasets due to file I/O. Benchmark against alternatives (e.g., spatie/laravel-settings with database).
  • Security:
    • File permissions must restrict access to the JSON file (e.g., storage/app should be writable only by the web server user).
    • Sensitive data (e.g., API keys) should use Laravel’s built-in encryption (config/encryption.php).
  • Testing:
    • Unit tests for key-value operations are straightforward.
    • Integration tests needed for edge cases (e.g., concurrent writes, file system failures).

Key Questions

  1. Data Size and Volume:
    • What’s the expected size of the JSON file? (e.g., <1MB is manageable; >100MB may degrade performance.)
    • How often will data be read/written? (e.g., per-request vs. batch operations.)
  2. Persistence Requirements:
    • Is data loss acceptable during crashes? (e.g., use flush() carefully in production.)
    • Are there backup/recovery needs? (e.g., versioned JSON files or database fallback.)
  3. Concurrency:
    • Will multiple processes/servers access the same Valuestore? (e.g., use a shared filesystem or database.)
  4. Alternatives:
    • Compare with:
      • Database: spatie/laravel-settings (structured, queryable).
      • Cache: Illuminate\Support\Facades\Cache (ephemeral, distributed).
      • Environment Config: .env (immutable, non-code).
  5. Monitoring:
    • How will file size/access patterns be monitored? (e.g., Laravel Horizon for file I/O metrics.)

Integration Approach

Stack Fit

  • Laravel Core: Native support for filesystem, caching, and service container.
  • PHP Extensions:
    • Filesystem: league/flysystem (if using cloud storage like S3).
    • Cache: spatie/laravel-caching (to cache the JSON file in memory).
  • Database Alternatives:
    • Use spatie/laravel-settings if relational data or querying is needed.
    • Use laravel/redis or predis/predis for distributed caching.
  • Testing:
    • Unit Tests: Mock Valuestore interface for isolated tests.
    • Feature Tests: Test file operations using Laravel’s Storage facade.

Migration Path

  1. Pilot Phase:
    • Start with non-critical data (e.g., feature flags, debug configs).
    • Use a separate JSON file (e.g., storage/app/debug-config.json) to avoid risk.
  2. Gradual Rollout:
    • Replace hardcoded configs with Valuestore (e.g., Valuestore::get('app.debug')).
    • Add caching layer for frequently accessed keys:
      $value = Cache::remember('valuestore_key', now()->addHours(1), function () {
          return Valuestore::get('key');
      });
      
  3. Fallback Strategy:
    • Implement a database-backed Valuestore for critical data (e.g., via Valuestore interface).
    • Use Laravel’s config() as a fallback for missing keys.

Compatibility

  • Laravel Versions: Tested with Laravel 10+ (check composer.json constraints).
  • PHP Versions: Requires PHP 8.1+ (align with Laravel’s requirements).
  • Filesystem:
    • Defaults to local storage; configure for S3/other backends via flysystem.
    • Ensure the storage path is writable and backed up.
  • Concurrency:
    • For multi-server setups, use a shared filesystem (e.g., NFS, EFS) or switch to a database.

Sequencing

  1. Setup:
    • Install package: composer require spatie/laravel-valuestore.
    • Publish config (if needed): php artisan vendor:publish --tag="valuestore-config".
    • Configure default path in config/valuestore.php:
      'path' => storage_path('app/valuestore.json'),
      
  2. Development:
    • Use in services/repositories:
      public function __construct(private Valuestore $valuestore) {}
      
    • Add to AppServiceProvider for global access:
      $this->app->singleton(Valuestore::class, function ($app) {
          return Valuestore::make($app['config']['valuestore.path']);
      });
      
  3. Production:
    • Monitor file size and access patterns.
    • Implement backups for the JSON file (e.g., cron job to copy to S3).
    • Consider adding a health check for file permissions.

Operational Impact

Maintenance

  • Pros:
    • No Database: No migrations, schema updates, or ORM complexity.
    • Self-Contained: Single file is easy to inspect/debug (tail -f storage/app/valuestore.json).
  • Cons:
    • Manual Backups: No built-in backup mechanism; rely on filesystem backups.
    • Permission Management: Ensure the web server user has write access to the file.
    • Versioning: No native support; implement custom logic (e.g., timestamped files).

Support

  • Debugging:
    • Log file operations for auditing:
      Valuestore::make($path)->put('key', $value);
      Log::debug('Valuestore updated', ['key' => 'key', 'value' => $value]);
      
    • Monitor for:
      • File permission errors (storage/app/valuestore.json not writable).
      • JSON parsing errors (malformed data).
  • Common Issues:
    • Concurrency: Race conditions during writes (mitigate with file locking).
    • Large Files: Performance degradation (mitigate with caching or database).
    • Data Loss: No rollback mechanism (mitigate with backups).

Scaling

  • Horizontal Scaling:
    • Challenge: Single-file storage is not shared across servers.
    • Solutions:
      • Use a shared filesystem (e.g., NFS, EFS) for multi-server setups.
      • Switch to a distributed cache (Redis) or database for high availability.
  • Vertical Scaling:
    • File I/O Bottlenecks: Large JSON files may slow down reads/writes.
    • Mitigations:
      • Cache frequently accessed keys in Laravel’s cache layer.
      • Split data across multiple JSON files (e.g., by prefix: storage/app/valuestore-*.json).
  • Performance:
    • Reads: Fast for small datasets (<1MB).
    • Writes: Slower due to file I/O; consider batching operations.

Failure Modes

Failure Scenario Impact Mitigation
File system full Writes fail, data loss Monitor disk space, set up alerts.
File permission denied No reads/writes Ensure correct permissions (chmod 644).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport