{"id":50770145,"url":"https://github.com/stevehill1981/sitemap_generator-cache_adapter","last_synced_at":"2026-06-11T17:31:58.741Z","repository":{"id":331608461,"uuid":"1131586562","full_name":"stevehill1981/sitemap_generator-cache_adapter","owner":"stevehill1981","description":"Cache adapter for sitemap_generator gem - stores sitemaps in Rails.cache for Kubernetes/Heroku","archived":false,"fork":false,"pushed_at":"2026-01-10T16:38:44.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-27T23:13:52.176Z","etag":null,"topics":["cache","kubernetes","rails","ruby","rubygems","sitemap"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/sitemap_generator-cache_adapter","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevehill1981.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-10T10:01:56.000Z","updated_at":"2026-01-10T16:38:47.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/stevehill1981/sitemap_generator-cache_adapter","commit_stats":null,"previous_names":["stevehill1981/sitemap_generator-cache_adapter"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/stevehill1981/sitemap_generator-cache_adapter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehill1981%2Fsitemap_generator-cache_adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehill1981%2Fsitemap_generator-cache_adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehill1981%2Fsitemap_generator-cache_adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehill1981%2Fsitemap_generator-cache_adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevehill1981","download_url":"https://codeload.github.com/stevehill1981/sitemap_generator-cache_adapter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevehill1981%2Fsitemap_generator-cache_adapter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34211061,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","kubernetes","rails","ruby","rubygems","sitemap"],"created_at":"2026-06-11T17:31:58.603Z","updated_at":"2026-06-11T17:31:58.696Z","avatar_url":"https://github.com/stevehill1981.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SitemapGenerator::CacheAdapter\n\nA cache-based storage adapter for the [sitemap_generator](https://github.com/kjvarga/sitemap_generator) gem. Stores sitemaps in `Rails.cache` instead of the filesystem.\n\n## Why?\n\nThe default `sitemap_generator` adapter writes sitemaps to disk. This doesn't work well for:\n\n- **Kubernetes deployments** with ephemeral pod storage\n- **Heroku** and other read-only filesystems\n- **Multi-pod deployments** where sitemaps need to be shared across instances\n- **Serverless environments** without persistent storage\n\nThis adapter stores sitemaps in your Rails cache instead. When used with a database-backed cache like [Solid Cache](https://github.com/rails/solid_cache), sitemaps persist across deploys and are shared across all application instances.\n\n## Installation\n\nAdd to your Gemfile:\n\n```ruby\ngem \"sitemap_generator-cache_adapter\"\n```\n\nThen run:\n\n```bash\nbundle install\n```\n\n## Usage\n\n### Basic Setup\n\nConfigure `sitemap_generator` to use the cache adapter in `config/sitemap.rb`:\n\n```ruby\nSitemapGenerator::Sitemap.default_host = \"https://example.com\"\nSitemapGenerator::Sitemap.adapter = SitemapGenerator::CacheAdapter.new\nSitemapGenerator::Sitemap.compress = false  # Recommended for cache storage\nSitemapGenerator::Sitemap.create_index = false  # Single sitemap file\n\nSitemapGenerator::Sitemap.create do\n  add \"/about\", changefreq: \"monthly\"\n  add \"/contact\", changefreq: \"monthly\"\n\n  # Add dynamic content\n  Post.find_each do |post|\n    add post_path(post), lastmod: post.updated_at\n  end\nend\n```\n\n### Serving Sitemaps (Single File)\n\nFor most sites, a single sitemap file is sufficient (up to 50,000 URLs).\n\nCreate a controller to serve sitemaps from the cache:\n\n```ruby\n# app/controllers/sitemaps_controller.rb\nclass SitemapsController \u003c ApplicationController\n  def show\n    xml = SitemapGenerator::CacheAdapter.fetch(\"sitemap.xml\") do\n      # Load the sitemap config, which runs create and caches the result\n      load Rails.root.join(\"config\", \"sitemap.rb\")\n      SitemapGenerator::Sitemap.ping_search_engines  # Optional\n    end\n\n    if xml.present?\n      render xml: xml\n    else\n      head :not_found\n    end\n  end\nend\n```\n\nAdd the route:\n\n```ruby\n# config/routes.rb\nget \"sitemap.xml\", to: \"sitemaps#show\", defaults: { format: :xml }\n```\n\n**Note**: Loading `config/sitemap.rb` executes the entire file, including the `create` block. This is how sitemap_generator is designed to work.\n\n### Serving Sitemaps (Multiple Files)\n\nFor large sites with more than 50,000 URLs, sitemap_generator creates multiple files with an index:\n\n```ruby\n# config/sitemap.rb\nSitemapGenerator::Sitemap.default_host = \"https://example.com\"\nSitemapGenerator::Sitemap.adapter = SitemapGenerator::CacheAdapter.new\nSitemapGenerator::Sitemap.compress = false\n# Don't set create_index = false\n\nSitemapGenerator::Sitemap.create do\n  # Your URLs here - will be split across multiple files if needed\nend\n```\n\nUpdate the controller to serve any sitemap file:\n\n```ruby\n# app/controllers/sitemaps_controller.rb\nclass SitemapsController \u003c ApplicationController\n  def show\n    filename = if params[:id].present?\n      \"sitemap#{params[:id]}.xml\"\n    else\n      \"sitemap.xml\"\n    end\n\n    xml = SitemapGenerator::CacheAdapter.fetch(filename) do\n      load Rails.root.join(\"config\", \"sitemap.rb\")\n      SitemapGenerator::Sitemap.ping_search_engines\n    end\n\n    if xml.present?\n      render xml: xml\n    else\n      head :not_found\n    end\n  end\nend\n```\n\nUpdate the routes:\n\n```ruby\n# config/routes.rb\nget \"sitemap.xml\", to: \"sitemaps#show\", defaults: { format: :xml }\nget \"sitemap:id.xml\", to: \"sitemaps#show\", defaults: { format: :xml }, constraints: { id: /[0-9]+/ }\n```\n\nThis serves:\n- `/sitemap.xml` - The sitemap index\n- `/sitemap1.xml`, `/sitemap2.xml`, etc. - Individual sitemap files\n\n### Configuration Options\n\n```ruby\nSitemapGenerator::Sitemap.adapter = SitemapGenerator::CacheAdapter.new(\n  cache_key_prefix: \"myapp:sitemap\",  # Default: \"sitemap_generator\"\n  expires_in: 12.hours,               # Default: 24.hours\n  cache_store: Rails.cache            # Default: Rails.cache\n)\n```\n\n### Class Methods\n\nThe adapter provides several class methods for working with cached sitemaps:\n\n```ruby\n# Fetch with lazy generation\nxml = SitemapGenerator::CacheAdapter.fetch(\"sitemap.xml\") do\n  SitemapGenerator::Sitemap.create\nend\n\n# Read directly (returns nil if not cached)\nxml = SitemapGenerator::CacheAdapter.read(\"sitemap.xml\")\n\n# Check existence\nif SitemapGenerator::CacheAdapter.exist?(\"sitemap.xml\")\n  # ...\nend\n\n# Delete a specific sitemap\nSitemapGenerator::CacheAdapter.delete(\"sitemap.xml\")\n\n# Clear all cached sitemaps\nSitemapGenerator::CacheAdapter.clear_all\n```\n\n## How It Works\n\n1. **On cache miss**: The `fetch` method yields to the block, which calls `SitemapGenerator::Sitemap.create`. This triggers the adapter's `write` method, storing the sitemap XML in the cache.\n\n2. **On cache hit**: The cached XML is returned immediately without regeneration.\n\n3. **Expiration**: Sitemaps expire after 24 hours by default. On the next request after expiration, a fresh sitemap is generated.\n\nThis \"lazy regeneration\" approach means:\n- No scheduled jobs required\n- Sitemaps are always fresh (within the cache TTL)\n- First request after expiration may be slightly slower\n\n## Cache Backend Compatibility\n\nWorks with any Rails cache backend:\n\n- **Solid Cache** (recommended for Kubernetes) - Database-backed, shared across pods\n- **Redis** - Fast, shared across pods\n- **Memcached** - Fast, shared across pods\n- **Memory Store** - Development/testing only (not shared)\n- **File Store** - Single-server deployments only\n\n## Example: Kubernetes with Solid Cache\n\n```ruby\n# Gemfile\ngem \"solid_cache\"\ngem \"sitemap_generator\"\ngem \"sitemap_generator-cache_adapter\"\n\n# config/environments/production.rb\nconfig.cache_store = :solid_cache_store\n\n# config/sitemap.rb\nSitemapGenerator::Sitemap.adapter = SitemapGenerator::CacheAdapter.new\n```\n\nWith this setup, sitemaps are stored in your database and accessible from any pod in your cluster.\n\n## Migrating from File-Based Sitemaps\n\n1. Add the gem to your Gemfile\n2. Update `config/sitemap.rb` to use the cache adapter\n3. Create the sitemaps controller and route\n4. Remove any sitemap-related cron jobs or scheduled tasks\n5. Add `public/sitemap*.xml` to `.gitignore`\n6. Delete existing static sitemap files\n\n## Development\n\n```bash\nbundle install\nbundle exec rspec\nbundle exec standardrb\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevehill1981%2Fsitemap_generator-cache_adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevehill1981%2Fsitemap_generator-cache_adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevehill1981%2Fsitemap_generator-cache_adapter/lists"}