Caching System
Run ID: 69cb2dd061b1021a29a869f62026-03-31Development
PantheraHive BOS
BOS Dashboard

Caching System Architecture Study Plan

This document outlines a comprehensive study plan designed to equip architects and engineers with a deep understanding of caching systems, from fundamental principles to advanced distributed architectures and practical implementation. This structured approach ensures a solid foundation for designing, implementing, and optimizing robust and scalable caching solutions.


1. Weekly Schedule

This 6-week program balances theoretical knowledge with practical application, progressing from core concepts to advanced system design and optimization.

  • Week 1: Fundamentals of Caching

* Topics:

* Introduction to Caching: Definition, purpose, benefits (performance, scalability, cost reduction).

* Key Metrics: Cache hit/miss ratio, latency, throughput, eviction rate.

* Types of Caching: In-memory, disk-based, database, distributed, CDN, browser.

* Cache Eviction Policies: LRU (Least Recently Used), LFU (Least Frequently Used), FIFO (First-In, First-Out), MRU (Most Recently Used), ARC (Adaptive Replacement Cache).

* Cache Invalidation Strategies: TTL (Time-To-Live), Write-Through, Write-Back, Write-Around, Cache-Aside.

* Basic Data Structures for Caching: Hash maps, doubly linked lists, heaps.

* Activities: Read foundational articles, watch introductory videos, understand trade-offs of different policies.

  • Week 2: In-Application and Local Caching

* Topics:

* In-Process/Local Caching: Using libraries like Guava Cache (Java), Ehcache (Java), .NET MemoryCache.

* Client-Side Caching: Browser caching, HTTP headers (Cache-Control, ETag, Last-Modified, Expires).

* Operating System Caching: File system cache, page cache, buffer cache.

* Designing Local Caches: Concurrency, thread safety, memory management, serialization.

* Use Cases: Caching frequently accessed configuration, session data, small lookup tables.

* Activities: Implement a simple LRU cache in your preferred language, experiment with browser cache headers using developer tools.

  • Week 3: Distributed Caching Systems

* Topics:

* Introduction to Distributed Caching: Why, benefits (scalability, high availability, shared data), challenges (consistency, network overhead).

* Architecture Patterns: Client-server, peer-to-peer, clustered.

* Data Partitioning and Sharding: Consistent hashing, range-based, hash-based.

* Replication and Fault Tolerance: Primary-replica, sharded replication.

* Popular Technologies: Redis (data structures, pub/sub, persistence), Memcached (simplicity, key-value store).

* Comparison: Redis vs. Memcached – features, performance, use cases.

* Activities: Set up and interact with a local Redis/Memcached instance, experiment with basic commands and data types.

  • Week 4: Advanced Caching Concepts & CDN

* Topics:

* Cache Coherency and Consistency Models: Strong consistency, eventual consistency, distributed cache invalidation.

* Solving Cache Stampede/Dog-Piling/Thundering Herd: Throttling, request coalescing, locking mechanisms.

* Pre-caching and Cache Warming: Strategies to pre-populate caches.

* Content Delivery Networks (CDNs): How they work, benefits (latency reduction, offloading origin), edge caching.

* Major CDN Providers: Akamai, Cloudflare, Amazon CloudFront, Google Cloud CDN, Azure CDN.

* Caching in Microservices Architectures: Dedicated caching services, API gateway caching.

* Activities: Research cache stampede solutions, evaluate CDN offerings for a hypothetical scenario.

  • Week 5: Database Caching and System Design

* Topics:

* Database-Level Caching: Query cache, result set cache, buffer pool, connection pooling.

* ORM Caching: Second-level caches (e.g., Hibernate, Entity Framework).

* Designing a Caching Layer: Identifying cacheable data, choosing appropriate strategies, capacity planning.

* System Design Case Studies: Applying caching to scenarios like e-commerce product catalogs, social media feeds, API rate limiting.

* Monitoring and Troubleshooting: Key metrics (hit ratio, eviction rate, memory usage, network I/O), common issues and debugging.

* Security Considerations: Sensitive data in cache, access control, encryption.

* Activities: Analyze a system design problem and propose a caching strategy, including technology choices and invalidation.

  • Week 6: Practical Implementation & Optimization

* Topics:

* Hands-on Lab: Integrate Redis/Memcached into a sample web application (e.g., using a cache-aside pattern).

* Performance Testing and Benchmarking: Tools and methodologies for evaluating cache performance.

* Optimizing Cache Usage: Identifying "hot" data, optimizing cache keys, minimizing cache misses.

* Cost Implications of Caching: Cloud service costs, memory consumption, operational overhead.

* Review of Best Practices and Anti-Patterns.

* Activities: Complete a practical coding assignment, conduct a performance test, present a caching optimization plan.


2. Learning Objectives

Upon successful completion of this study plan, participants will be able to:

  • Understand Fundamental Caching Principles: Define caching, articulate its benefits and trade-offs, and differentiate between various cache types and eviction policies (e.g., LRU, LFU, FIFO).
  • Analyze and Select Appropriate Caching Strategies: Evaluate different invalidation strategies (e.g., cache-aside, write-through, TTL) and their suitability for specific application requirements and data characteristics.
  • Design and Implement Local Caching Solutions: Apply in-memory caching techniques using standard libraries (e.g., Guava Cache, Ehcache, .NET MemoryCache) to improve application performance and responsiveness.
  • Architect and Integrate Distributed Caching Systems: Design scalable and highly available caching layers using technologies like Redis or Memcached, incorporating data partitioning, replication, and fault tolerance mechanisms.
  • Optimize and Troubleshoot Caching Performance: Monitor key caching metrics (e.g., hit ratio, latency, eviction rate), identify performance bottlenecks, and apply optimization techniques to enhance system efficiency.
  • Evaluate and Select CDN Solutions: Understand the role of Content Delivery Networks (CDNs) in global content delivery, edge caching, and choose appropriate providers based on project needs and geographic distribution.
  • Address Advanced Caching Challenges: Develop strategies to ensure cache coherency, prevent cache stampede/dog-piling, implement secure caching practices, and design for pre-caching/warming.
  • Apply Caching in System Design: Integrate caching effectively into broader system architectures, including microservices and database interactions, making informed decisions on technology stack and design patterns.

3. Recommended Resources

A curated list of resources to support learning and practical application.

  • Books:

* "Designing Data-Intensive Applications" by Martin Kleppmann: Essential for understanding distributed systems, consistency, and caching patterns.

* "System Design Interview – An insider's guide" by Alex Xu: Contains practical examples and explanations of caching in system design.

* "Redis in Action" by Josiah L. Carlson: Deep dive into Redis features and use cases.

  • Online Courses & Platforms:

* Educative.io - "Grokking the System Design Interview": Dedicated sections on caching concepts and patterns.

* Coursera/edX - Distributed Systems Specializations: Courses from universities like Cornell, Georgia Tech often cover caching in depth.

* Redis University (university.redis.com): Official free courses on Redis fundamentals and advanced topics.

* Pluralsight/Udemy: Various courses on specific caching technologies (e.g., "Mastering Redis," "Caching in .NET").

  • Documentation & Official Guides:

* Redis Official Documentation: Comprehensive and up-to-date.

* Memcached Official Documentation: Simple and clear.

* Guava Cache & Ehcache Documentation: For in-process Java caching.

* AWS CloudFront, Google Cloud CDN, Azure CDN Documentation: For understanding CDN services.

  • Articles & Blogs:

* High Scalability Blog (highscalability.com): Real-world case studies and architectural patterns.

* Martin Fowler's Articles: Search for "caching patterns" for conceptual understanding.

* Engineering Blogs: Netflix TechBlog, Facebook Engineering, Google Cloud Blog often publish articles on their caching strategies and innovations.

  • Hands-on Tools & Environments:

* Docker: For easily setting up and experimenting with Redis, Memcached, and other services locally.

* Postman/Insomnia: For testing HTTP caching headers.

* Cloud Provider Free Tiers: AWS, GCP, Azure offer free tiers for managed caching services (e.g., AWS ElastiCache, Azure Cache for Redis) to experiment with.

* Performance Testing Tools: Apache JMeter, K6, Locust for benchmarking caching solutions.


4. Milestones

Key checkpoints to track progress and ensure mastery of the material.

  • End of Week 1: Successfully articulate the core concepts of caching, including types, benefits, and common eviction/invalidation policies.
  • End of Week 2: Demonstrate the ability to implement and configure a basic in-application cache and explain client-side caching mechanisms.
  • End of Week 3: Proficiently set up and interact with a distributed caching system (Redis/Memcached), understanding its architecture and data partitioning.

*

gemini Output

This document provides a comprehensive, detailed, and professional output for a Caching System, specifically focusing on a production-ready implementation. This deliverable includes a robust Python-based Least Recently Used (LRU) cache with Time-To-Live (TTL) functionality and thread safety, along with thorough explanations and usage instructions.


1. Introduction to Caching Systems

A caching system is a high-speed data storage layer that stores a subset of data, typically transient in nature, so that future requests for that data can be served up faster than by accessing the data's primary storage location. Caching allows you to efficiently reuse previously fetched or computed data.

Benefits of Caching:

  • Performance Improvement: Dramatically reduces data retrieval times by serving data from fast
gemini Output

Caching System: Comprehensive Review and Documentation

This document provides a comprehensive overview of Caching Systems, detailing their purpose, benefits, key concepts, implementation considerations, and best practices. It serves as a foundational guide for understanding, designing, and optimizing caching strategies within your applications and infrastructure.


1. Executive Summary

Caching is a fundamental technique used to improve application performance, reduce database and API load, and enhance user experience by storing frequently accessed data in a faster, more readily available location. By intelligently leveraging caching, systems can achieve higher throughput, lower latency, and greater scalability, leading to significant operational cost savings and improved customer satisfaction. This document outlines the critical aspects of implementing and managing an effective caching system.


2. Understanding Caching Systems

2.1 What is Caching?

Caching involves storing copies of data that are expensive to retrieve or compute, in a temporary, high-speed storage layer (the "cache"). When a request for this data comes in, the system first checks the cache. If the data is found (a "cache hit"), it's returned immediately, bypassing the original data source. If not found (a "cache miss"), the data is retrieved from its primary source, served to the requester, and then typically stored in the cache for future requests.

2.2 Why is Caching Important?

  • Performance Improvement: Significantly reduces data retrieval times, leading to faster response times for users.
  • Reduced Load on Backend Systems: Offloads requests from databases, APIs, and other expensive services, preventing overload and improving their stability.
  • Scalability: Allows applications to handle a much larger volume of requests without needing to scale up primary data sources proportionally.
  • Cost Efficiency: Reduces the need for expensive database resources or higher-tier compute instances by serving data from cheaper, faster cache layers.
  • Enhanced User Experience: Faster loading times and more responsive applications directly translate to a better experience for end-users.
  • Resilience: Can provide a layer of resilience by serving stale data from the cache if the primary data source becomes temporarily unavailable.

3. Key Components and Concepts

3.1 Cache Store

The actual storage mechanism for cached data. Common choices include:

  • In-Memory Cache: Stores data directly in the application's RAM (e.g., Guava Cache, ConcurrentHashMap). Fastest but limited to a single application instance and volatile.
  • Distributed Cache: A network-accessible cache that can be shared across multiple application instances (e.g., Redis, Memcached). Offers scalability and high availability.
  • Content Delivery Networks (CDNs): Geographically distributed servers that cache static and dynamic content closer to users.
  • Browser/Client-Side Cache: Caches content directly in the user's web browser.

3.2 Cache Key

A unique identifier used to store and retrieve data from the cache. Keys should be deterministic, descriptive, and consistent.

3.3 Cache Value

The actual data stored in the cache, associated with a cache key.

3.4 Cache Invalidation Strategies

The process of removing or updating stale data from the cache. This is one of the most critical and challenging aspects of caching.

  • Time-To-Live (TTL): Data expires after a fixed duration. Simple but can lead to serving stale data until expiration.
  • Least Recently Used (LRU): When the cache is full, the item that hasn't been accessed for the longest time is removed.
  • Least Frequently Used (LFU): When the cache is full, the item with the fewest accesses is removed.
  • Write-Through: Data is written to both the cache and the primary data store simultaneously. Ensures data consistency but adds latency to writes.
  • Write-Back: Data is written only to the cache initially, and then asynchronously written to the primary data store. Faster writes but risk of data loss if cache fails before write-back.
  • Cache-Aside (Lazy Loading): Application checks cache first. If a miss, it fetches from the primary store, serves the data, and then populates the cache. Most common strategy.
  • Publish/Subscribe (Pub/Sub) Invalidation: When data changes in the primary store, a message is published, and all relevant cache instances subscribe to invalidate their copies.

3.5 Cache Coherency

Ensuring that all clients or application instances see the most up-to-date version of cached data. This is particularly challenging in distributed systems.

3.6 Cache Hit/Miss Ratio

  • Cache Hit: A request for data is successfully served from the cache.
  • Cache Miss: A request for data is not found in the cache and must be retrieved from the primary source.
  • Hit Ratio: The percentage of requests served from the cache (Hits / Total Requests). A high hit ratio indicates an effective caching strategy.

4. Common Caching Patterns and Architectures

4.1 Client-Side Caching (Browser/CDN)

  • Description: Caching data directly on the user's device (browser cache) or at edge locations geographically close to the user (CDN).
  • Use Cases: Static assets (images, CSS, JavaScript), public API responses, rendered HTML pages.
  • Technologies: HTTP caching headers (Cache-Control, ETag, Last-Modified), CDN services (Cloudflare, Akamai, AWS CloudFront).

4.2 Application-Level Caching

  • Description: Caching data within or close to the application layer. Can be in-memory or a separate distributed cache service.
  • Use Cases: Frequently accessed database query results, computed values, user sessions, API rate limits.
  • Technologies:

* In-Memory: Application-specific libraries (e.g., Caffeine, Ehcache for Java; cachetools for Python).

* Distributed: Redis, Memcached, Apache Ignite, Hazelcast.

4.3 Database Caching

  • Description: Caching mechanisms built into or layered on top of database systems.
  • Use Cases: Query result caching, object-relational mapper (ORM) caches, materialized views.
  • Technologies: Database-specific caches (e.g., PostgreSQL shared buffers, MySQL query cache), ORM second-level caches (Hibernate, SQLAlchemy).

4.4 API Gateway Caching

  • Description: Caching API responses at the API Gateway layer before they reach backend services.
  • Use Cases: Public APIs with high read traffic, reducing load on microservices.
  • Technologies: AWS API Gateway caching, NGINX reverse proxy caching, Apigee, Kong.

5. Implementation Considerations

5.1 Choosing a Cache Technology

| Technology | Pros | Cons | Best For |

| :--------------- | :---------------------------------------------------------------- | :----------------------------------------------------------------- | :---------------------------------------------------------------------------- |

| Redis | Versatile (key-value, data structures), high performance, persistence options, pub/sub, transactions, replication, clustering. | Can be complex to operate at scale for self-hosted. | General-purpose distributed cache, session store, real-time analytics, message broker. |

| Memcached | Very fast, simple, lightweight, easy to scale horizontally. | Only key-value store, no persistence, no complex data structures. | Simple object caching, session caching where data loss is acceptable. |

| In-Memory | Extremely fast (no network hop), simple to integrate. | Volatile, limited to single process, no shared state. | Caching small, frequently accessed, short-lived data within a single application instance. |

| CDN | Global distribution, reduces latency for static assets, offloads origin. | Primarily for static/semi-static content, complex invalidation for dynamic. | Static assets, media files, geographically distributed users. |

5.2 Data Granularity

Decide what to cache. Cache small, frequently accessed, immutable, or slowly changing data. Avoid caching large, constantly changing objects or sensitive information without proper encryption.

5.3 Cache Sizing and Capacity Planning

  • Determine Cache Size: Estimate the amount of data to be cached based on expected hit ratio and data eviction policies.
  • Memory vs. CPU: Caching is memory-intensive. Ensure sufficient RAM for the cache store.
  • Network Bandwidth: For distributed caches, consider network latency and bandwidth between application servers and the cache.

5.4 Deployment Models

  • Self-Hosted: Full control, but requires significant operational overhead for setup, monitoring, scaling, and maintenance.
  • Managed Service: (e.g., AWS ElastiCache, Azure Cache for Redis, Google Cloud Memorystore) Reduced operational burden, high availability, automated backups, easier scaling. Recommended for most production environments.

5.5 Security

  • Network Isolation: Deploy cache instances in private subnets, restrict access via security groups/firewalls.
  • Authentication/Authorization: Use strong authentication for cache access (e.g., Redis AUTH password).
  • Encryption: Encrypt data in transit (TLS/SSL) and at rest (if supported and necessary for sensitive data).
  • Sensitive Data: Avoid caching highly sensitive PII or financial data unless absolutely necessary and with robust security measures.

6. Best Practices for Effective Caching

6.1 Identify Hotspots

Analyze application logs, database queries, and API calls to identify the most frequently accessed data or computationally expensive operations that would benefit most from caching.

6.2 Use Appropriate Invalidation Strategies

  • TTL First: Start with a reasonable TTL. It's the simplest strategy.
  • Event-Driven Invalidation: For critical data, use a pub/sub mechanism to invalidate cache entries immediately when the source data changes.
  • Stale-While-Revalidate: Serve stale data from the cache while asynchronously fetching fresh data from the origin to update the cache.

6.3 Handle Cache Stampedes (Thundering Herd)

When a cache entry expires or is invalidated, multiple concurrent requests might all miss the cache and hit the backend simultaneously, overloading it.

  • Locking: Use a distributed lock to ensure only one request rebuilds the cache for a given key.
  • Probabilistic Early Expiration: Expire items slightly before their actual TTL, then asynchronously refresh them.
  • Stale-While-Revalidate: As mentioned above, serve stale content while refreshing in the background.

6.4 Implement Fallbacks and Graceful Degradation

What happens if the cache is unavailable or returns an error?

  • Circuit Breaker: Implement a circuit breaker pattern to prevent cascading failures if the cache is down.
  • Direct to Origin: If the cache fails, fall back to fetching data directly from the primary data source (with appropriate rate limiting/circuit breaking on the origin).
  • Serve Stale Data (if acceptable): In some cases, serving slightly stale data is better than an error.

6.5 Monitor Cache Performance

Continuously monitor key metrics to ensure the cache is performing as expected and to identify areas for optimization.

6.6 Test Thoroughly

  • Unit Tests: Test cache interactions (put, get, invalidate).
  • Integration Tests: Test how the application behaves with and without the cache, and during cache invalidation.
  • Load Testing: Simulate high traffic to validate cache performance, hit ratio, and backend load reduction.

7. Monitoring and Maintenance

Effective monitoring is crucial for maintaining a healthy and performant caching system.

7.1 Key Metrics to Monitor

  • Cache Hit Ratio: The most important metric. Aim for consistently high ratios (e.g., >80-90%).
  • Cache Miss Rate: Indicates how often the cache is not providing data.
  • Evictions: Number of items removed due to memory pressure or TTL expiration. High evictions might indicate insufficient cache size.
  • Latency: Time taken to retrieve data from the cache.
  • Memory Usage: Current and peak memory consumption of the cache store.
  • Network I/O: Inbound/outbound traffic for distributed caches.
  • CPU Usage: For the cache server instances.
  • Error Rates: Any errors reported by the cache system.

7.2 Tools and Dashboards

  • Cloud Provider Monitoring: AWS CloudWatch, Azure Monitor, Google Cloud Monitoring provide metrics for managed cache services.
  • Open Source Tools: Prometheus + Grafana, Datadog, New Relic for custom dashboards and alerts.
  • Cache-Specific Clients: Many cache technologies offer client libraries with built-in monitoring capabilities.

7.3 Regular Review and Optimization

  • TTL Adjustment: Periodically review and adjust TTLs based on data volatility and access patterns.
  • Key Design: Optimize cache key generation for better distribution and hit rates.
  • Capacity Planning: Re-evaluate cache size and instance types as application usage patterns evolve.
  • Invalidation Strategy Review: Ensure invalidation strategies remain effective and do not introduce data inconsistencies.

8. Conclusion and Next Steps

Implementing a robust caching system is a powerful strategy to enhance application performance, scalability, and user experience. By carefully considering the various types of caching, selecting appropriate technologies, and adhering to best practices for implementation and monitoring, your organization can unlock significant efficiencies and deliver superior service.

Recommended Next Steps:

  1. Identify Specific Use Cases: Pinpoint the top 2-3 areas in your current applications that would benefit most from caching.
  2. Technology Selection: Based on identified use cases and existing infrastructure, select a primary caching technology (e.g., Redis via a managed service).
  3. Proof of Concept (PoC): Implement a small-scale PoC for one identified use case to validate the chosen technology and strategy.
  4. Monitoring Setup: Establish initial monitoring dashboards and alerts for the PoC.
  5. Documentation & Training: Document the chosen caching patterns and provide training to development teams on best practices.

We are ready to assist you in each of these steps, from architectural design to implementation and ongoing optimization.

caching_system.md
Download as Markdown
Copy all content
Full output as text
Download ZIP
IDE-ready project ZIP
Copy share link
Permanent URL for this run
Get Embed Code
Embed this result on any website
Print / Save PDF
Use browser print dialog
\n\n\n"); var hasSrcMain=Object.keys(extracted).some(function(k){return k.indexOf("src/main")>=0;}); if(!hasSrcMain) zip.file(folder+"src/main."+ext,"import React from 'react'\nimport ReactDOM from 'react-dom/client'\nimport App from './App'\nimport './index.css'\n\nReactDOM.createRoot(document.getElementById('root')!).render(\n \n \n \n)\n"); var hasSrcApp=Object.keys(extracted).some(function(k){return k==="src/App."+ext||k==="App."+ext;}); if(!hasSrcApp) zip.file(folder+"src/App."+ext,"import React from 'react'\nimport './App.css'\n\nfunction App(){\n return(\n
\n
\n

"+slugTitle(pn)+"

\n

Built with PantheraHive BOS

\n
\n
\n )\n}\nexport default App\n"); zip.file(folder+"src/index.css","*{margin:0;padding:0;box-sizing:border-box}\nbody{font-family:system-ui,-apple-system,sans-serif;background:#f0f2f5;color:#1a1a2e}\n.app{min-height:100vh;display:flex;flex-direction:column}\n.app-header{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;padding:40px}\nh1{font-size:2.5rem;font-weight:700}\n"); zip.file(folder+"src/App.css",""); zip.file(folder+"src/components/.gitkeep",""); zip.file(folder+"src/pages/.gitkeep",""); zip.file(folder+"src/hooks/.gitkeep",""); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\nnpm run dev\n\`\`\`\n\n## Build\n\`\`\`bash\nnpm run build\n\`\`\`\n\n## Open in IDE\nOpen the project folder in VS Code or WebStorm.\n"); zip.file(folder+".gitignore","node_modules/\ndist/\n.env\n.DS_Store\n*.local\n"); } /* --- Vue (Vite + Composition API + TypeScript) --- */ function buildVue(zip,folder,app,code,panelTxt){ var pn=pkgName(app); var C=cc(pn); var extracted=extractCode(panelTxt); zip.file(folder+"package.json",'{\n "name": "'+pn+'",\n "version": "0.0.0",\n "type": "module",\n "scripts": {\n "dev": "vite",\n "build": "vue-tsc -b && vite build",\n "preview": "vite preview"\n },\n "dependencies": {\n "vue": "^3.5.13",\n "vue-router": "^4.4.5",\n "pinia": "^2.3.0",\n "axios": "^1.7.9"\n },\n "devDependencies": {\n "@vitejs/plugin-vue": "^5.2.1",\n "typescript": "~5.7.3",\n "vite": "^6.0.5",\n "vue-tsc": "^2.2.0"\n }\n}\n'); zip.file(folder+"vite.config.ts","import { defineConfig } from 'vite'\nimport vue from '@vitejs/plugin-vue'\nimport { resolve } from 'path'\n\nexport default defineConfig({\n plugins: [vue()],\n resolve: { alias: { '@': resolve(__dirname,'src') } }\n})\n"); zip.file(folder+"tsconfig.json",'{"files":[],"references":[{"path":"./tsconfig.app.json"},{"path":"./tsconfig.node.json"}]}\n'); zip.file(folder+"tsconfig.app.json",'{\n "compilerOptions":{\n "target":"ES2020","useDefineForClassFields":true,"module":"ESNext","lib":["ES2020","DOM","DOM.Iterable"],\n "skipLibCheck":true,"moduleResolution":"bundler","allowImportingTsExtensions":true,\n "isolatedModules":true,"moduleDetection":"force","noEmit":true,"jsxImportSource":"vue",\n "strict":true,"paths":{"@/*":["./src/*"]}\n },\n "include":["src/**/*.ts","src/**/*.d.ts","src/**/*.tsx","src/**/*.vue"]\n}\n'); zip.file(folder+"env.d.ts","/// \n"); zip.file(folder+"index.html","\n\n\n \n \n "+slugTitle(pn)+"\n\n\n
\n \n\n\n"); var hasMain=Object.keys(extracted).some(function(k){return k==="src/main.ts"||k==="main.ts";}); if(!hasMain) zip.file(folder+"src/main.ts","import { createApp } from 'vue'\nimport { createPinia } from 'pinia'\nimport App from './App.vue'\nimport './assets/main.css'\n\nconst app = createApp(App)\napp.use(createPinia())\napp.mount('#app')\n"); var hasApp=Object.keys(extracted).some(function(k){return k.indexOf("App.vue")>=0;}); if(!hasApp) zip.file(folder+"src/App.vue","\n\n\n\n\n"); zip.file(folder+"src/assets/main.css","*{margin:0;padding:0;box-sizing:border-box}body{font-family:system-ui,sans-serif;background:#fff;color:#213547}\n"); zip.file(folder+"src/components/.gitkeep",""); zip.file(folder+"src/views/.gitkeep",""); zip.file(folder+"src/stores/.gitkeep",""); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\nnpm run dev\n\`\`\`\n\n## Build\n\`\`\`bash\nnpm run build\n\`\`\`\n\nOpen in VS Code or WebStorm.\n"); zip.file(folder+".gitignore","node_modules/\ndist/\n.env\n.DS_Store\n*.local\n"); } /* --- Angular (v19 standalone) --- */ function buildAngular(zip,folder,app,code,panelTxt){ var pn=pkgName(app); var C=cc(pn); var sel=pn.replace(/_/g,"-"); var extracted=extractCode(panelTxt); zip.file(folder+"package.json",'{\n "name": "'+pn+'",\n "version": "0.0.0",\n "scripts": {\n "ng": "ng",\n "start": "ng serve",\n "build": "ng build",\n "test": "ng test"\n },\n "dependencies": {\n "@angular/animations": "^19.0.0",\n "@angular/common": "^19.0.0",\n "@angular/compiler": "^19.0.0",\n "@angular/core": "^19.0.0",\n "@angular/forms": "^19.0.0",\n "@angular/platform-browser": "^19.0.0",\n "@angular/platform-browser-dynamic": "^19.0.0",\n "@angular/router": "^19.0.0",\n "rxjs": "~7.8.0",\n "tslib": "^2.3.0",\n "zone.js": "~0.15.0"\n },\n "devDependencies": {\n "@angular-devkit/build-angular": "^19.0.0",\n "@angular/cli": "^19.0.0",\n "@angular/compiler-cli": "^19.0.0",\n "typescript": "~5.6.0"\n }\n}\n'); zip.file(folder+"angular.json",'{\n "$schema": "./node_modules/@angular/cli/lib/config/schema.json",\n "version": 1,\n "newProjectRoot": "projects",\n "projects": {\n "'+pn+'": {\n "projectType": "application",\n "root": "",\n "sourceRoot": "src",\n "prefix": "app",\n "architect": {\n "build": {\n "builder": "@angular-devkit/build-angular:application",\n "options": {\n "outputPath": "dist/'+pn+'",\n "index": "src/index.html",\n "browser": "src/main.ts",\n "tsConfig": "tsconfig.app.json",\n "styles": ["src/styles.css"],\n "scripts": []\n }\n },\n "serve": {"builder":"@angular-devkit/build-angular:dev-server","configurations":{"production":{"buildTarget":"'+pn+':build:production"},"development":{"buildTarget":"'+pn+':build:development"}},"defaultConfiguration":"development"}\n }\n }\n }\n}\n'); zip.file(folder+"tsconfig.json",'{\n "compileOnSave": false,\n "compilerOptions": {"baseUrl":"./","outDir":"./dist/out-tsc","forceConsistentCasingInFileNames":true,"strict":true,"noImplicitOverride":true,"noPropertyAccessFromIndexSignature":true,"noImplicitReturns":true,"noFallthroughCasesInSwitch":true,"paths":{"@/*":["src/*"]},"skipLibCheck":true,"esModuleInterop":true,"sourceMap":true,"declaration":false,"experimentalDecorators":true,"moduleResolution":"bundler","importHelpers":true,"target":"ES2022","module":"ES2022","useDefineForClassFields":false,"lib":["ES2022","dom"]},\n "references":[{"path":"./tsconfig.app.json"}]\n}\n'); zip.file(folder+"tsconfig.app.json",'{\n "extends":"./tsconfig.json",\n "compilerOptions":{"outDir":"./dist/out-tsc","types":[]},\n "files":["src/main.ts"],\n "include":["src/**/*.d.ts"]\n}\n'); zip.file(folder+"src/index.html","\n\n\n \n "+slugTitle(pn)+"\n \n \n \n\n\n \n\n\n"); zip.file(folder+"src/main.ts","import { bootstrapApplication } from '@angular/platform-browser';\nimport { appConfig } from './app/app.config';\nimport { AppComponent } from './app/app.component';\n\nbootstrapApplication(AppComponent, appConfig)\n .catch(err => console.error(err));\n"); zip.file(folder+"src/styles.css","* { margin: 0; padding: 0; box-sizing: border-box; }\nbody { font-family: system-ui, -apple-system, sans-serif; background: #f9fafb; color: #111827; }\n"); var hasComp=Object.keys(extracted).some(function(k){return k.indexOf("app.component")>=0;}); if(!hasComp){ zip.file(folder+"src/app/app.component.ts","import { Component } from '@angular/core';\nimport { RouterOutlet } from '@angular/router';\n\n@Component({\n selector: 'app-root',\n standalone: true,\n imports: [RouterOutlet],\n templateUrl: './app.component.html',\n styleUrl: './app.component.css'\n})\nexport class AppComponent {\n title = '"+pn+"';\n}\n"); zip.file(folder+"src/app/app.component.html","
\n
\n

"+slugTitle(pn)+"

\n

Built with PantheraHive BOS

\n
\n \n
\n"); zip.file(folder+"src/app/app.component.css",".app-header{display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:60vh;gap:16px}h1{font-size:2.5rem;font-weight:700;color:#6366f1}\n"); } zip.file(folder+"src/app/app.config.ts","import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';\nimport { provideRouter } from '@angular/router';\nimport { routes } from './app.routes';\n\nexport const appConfig: ApplicationConfig = {\n providers: [\n provideZoneChangeDetection({ eventCoalescing: true }),\n provideRouter(routes)\n ]\n};\n"); zip.file(folder+"src/app/app.routes.ts","import { Routes } from '@angular/router';\n\nexport const routes: Routes = [];\n"); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\nng serve\n# or: npm start\n\`\`\`\n\n## Build\n\`\`\`bash\nng build\n\`\`\`\n\nOpen in VS Code with Angular Language Service extension.\n"); zip.file(folder+".gitignore","node_modules/\ndist/\n.env\n.DS_Store\n*.local\n.angular/\n"); } /* --- Python --- */ function buildPython(zip,folder,app,code){ var title=slugTitle(app); var pn=pkgName(app); var src=code.replace(/^\`\`\`[\w]*\n?/m,"").replace(/\n?\`\`\`$/m,"").trim(); var reqMap={"numpy":"numpy","pandas":"pandas","sklearn":"scikit-learn","tensorflow":"tensorflow","torch":"torch","flask":"flask","fastapi":"fastapi","uvicorn":"uvicorn","requests":"requests","sqlalchemy":"sqlalchemy","pydantic":"pydantic","dotenv":"python-dotenv","PIL":"Pillow","cv2":"opencv-python","matplotlib":"matplotlib","seaborn":"seaborn","scipy":"scipy"}; var reqs=[]; Object.keys(reqMap).forEach(function(k){if(src.indexOf("import "+k)>=0||src.indexOf("from "+k)>=0)reqs.push(reqMap[k]);}); var reqsTxt=reqs.length?reqs.join("\n"):"# add dependencies here\n"; zip.file(folder+"main.py",src||"# "+title+"\n# Generated by PantheraHive BOS\n\nprint(title+\" loaded\")\n"); zip.file(folder+"requirements.txt",reqsTxt); zip.file(folder+".env.example","# Environment variables\n"); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\npython3 -m venv .venv\nsource .venv/bin/activate\npip install -r requirements.txt\n\`\`\`\n\n## Run\n\`\`\`bash\npython main.py\n\`\`\`\n"); zip.file(folder+".gitignore",".venv/\n__pycache__/\n*.pyc\n.env\n.DS_Store\n"); } /* --- Node.js --- */ function buildNode(zip,folder,app,code){ var title=slugTitle(app); var pn=pkgName(app); var src=code.replace(/^\`\`\`[\w]*\n?/m,"").replace(/\n?\`\`\`$/m,"").trim(); var depMap={"mongoose":"^8.0.0","dotenv":"^16.4.5","axios":"^1.7.9","cors":"^2.8.5","bcryptjs":"^2.4.3","jsonwebtoken":"^9.0.2","socket.io":"^4.7.4","uuid":"^9.0.1","zod":"^3.22.4","express":"^4.18.2"}; var deps={}; Object.keys(depMap).forEach(function(k){if(src.indexOf(k)>=0)deps[k]=depMap[k];}); if(!deps["express"])deps["express"]="^4.18.2"; var pkgJson=JSON.stringify({"name":pn,"version":"1.0.0","main":"src/index.js","scripts":{"start":"node src/index.js","dev":"nodemon src/index.js"},"dependencies":deps,"devDependencies":{"nodemon":"^3.0.3"}},null,2)+"\n"; zip.file(folder+"package.json",pkgJson); var fallback="const express=require(\"express\");\nconst app=express();\napp.use(express.json());\n\napp.get(\"/\",(req,res)=>{\n res.json({message:\""+title+" API\"});\n});\n\nconst PORT=process.env.PORT||3000;\napp.listen(PORT,()=>console.log(\"Server on port \"+PORT));\n"; zip.file(folder+"src/index.js",src||fallback); zip.file(folder+".env.example","PORT=3000\n"); zip.file(folder+".gitignore","node_modules/\n.env\n.DS_Store\n"); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\n## Setup\n\`\`\`bash\nnpm install\n\`\`\`\n\n## Run\n\`\`\`bash\nnpm run dev\n\`\`\`\n"); } /* --- Vanilla HTML --- */ function buildVanillaHtml(zip,folder,app,code){ var title=slugTitle(app); var isFullDoc=code.trim().toLowerCase().indexOf("=0||code.trim().toLowerCase().indexOf("=0; var indexHtml=isFullDoc?code:"\n\n\n\n\n"+title+"\n\n\n\n"+code+"\n\n\n\n"; zip.file(folder+"index.html",indexHtml); zip.file(folder+"style.css","/* "+title+" — styles */\n*{margin:0;padding:0;box-sizing:border-box}\nbody{font-family:system-ui,-apple-system,sans-serif;background:#fff;color:#1a1a2e}\n"); zip.file(folder+"script.js","/* "+title+" — scripts */\n"); zip.file(folder+"assets/.gitkeep",""); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\n## Open\nDouble-click \`index.html\` in your browser.\n\nOr serve locally:\n\`\`\`bash\nnpx serve .\n# or\npython3 -m http.server 3000\n\`\`\`\n"); zip.file(folder+".gitignore",".DS_Store\nnode_modules/\n.env\n"); } /* ===== MAIN ===== */ var sc=document.createElement("script"); sc.src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"; sc.onerror=function(){ if(lbl)lbl.textContent="Download ZIP"; alert("JSZip load failed — check connection."); }; sc.onload=function(){ var zip=new JSZip(); var base=(_phFname||"output").replace(/\.[^.]+$/,""); var app=base.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"")||"my_app"; var folder=app+"/"; var vc=document.getElementById("panel-content"); var panelTxt=vc?(vc.innerText||vc.textContent||""):""; var lang=detectLang(_phCode,panelTxt); if(_phIsHtml){ buildVanillaHtml(zip,folder,app,_phCode); } else if(lang==="flutter"){ buildFlutter(zip,folder,app,_phCode,panelTxt); } else if(lang==="react-native"){ buildReactNative(zip,folder,app,_phCode,panelTxt); } else if(lang==="swift"){ buildSwift(zip,folder,app,_phCode,panelTxt); } else if(lang==="kotlin"){ buildKotlin(zip,folder,app,_phCode,panelTxt); } else if(lang==="react"){ buildReact(zip,folder,app,_phCode,panelTxt); } else if(lang==="vue"){ buildVue(zip,folder,app,_phCode,panelTxt); } else if(lang==="angular"){ buildAngular(zip,folder,app,_phCode,panelTxt); } else if(lang==="python"){ buildPython(zip,folder,app,_phCode); } else if(lang==="node"){ buildNode(zip,folder,app,_phCode); } else { /* Document/content workflow */ var title=app.replace(/_/g," "); var md=_phAll||_phCode||panelTxt||"No content"; zip.file(folder+app+".md",md); var h=""+title+""; h+="

"+title+"

"; var hc=md.replace(/&/g,"&").replace(//g,">"); hc=hc.replace(/^### (.+)$/gm,"

$1

"); hc=hc.replace(/^## (.+)$/gm,"

$1

"); hc=hc.replace(/^# (.+)$/gm,"

$1

"); hc=hc.replace(/\*\*(.+?)\*\*/g,"$1"); hc=hc.replace(/\n{2,}/g,"

"); h+="

"+hc+"

Generated by PantheraHive BOS
"; zip.file(folder+app+".html",h); zip.file(folder+"README.md","# "+title+"\n\nGenerated by PantheraHive BOS.\n\nFiles:\n- "+app+".md (Markdown)\n- "+app+".html (styled HTML)\n"); } zip.generateAsync({type:"blob"}).then(function(blob){ var a=document.createElement("a"); a.href=URL.createObjectURL(blob); a.download=app+".zip"; a.click(); URL.revokeObjectURL(a.href); if(lbl)lbl.textContent="Download ZIP"; }); }; document.head.appendChild(sc); } function phShare(){navigator.clipboard.writeText(window.location.href).then(function(){var el=document.getElementById("ph-share-lbl");if(el){el.textContent="Link copied!";setTimeout(function(){el.textContent="Copy share link";},2500);}});}function phEmbed(){var runId=window.location.pathname.split("/").pop().replace(".html","");var embedUrl="https://pantherahive.com/embed/"+runId;var code='';navigator.clipboard.writeText(code).then(function(){var el=document.getElementById("ph-embed-lbl");if(el){el.textContent="Embed code copied!";setTimeout(function(){el.textContent="Get Embed Code";},2500);}});}