• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Please visit https://github.com/fastly/fastly-ruby.

Fastly Rails Plugin Build Status

Deprecation Notice

This repository has been deprecated. The official Fastly Ruby client is the fastly-ruby gem.

Although this repository is no longer supported, it could be useful as a reference.


Introduction

Fastly dynamic caching integration for Rails.

To use Fastly dynamic caching, you tag any response you wish to cache with unique Surrogate-Key HTTP Header(s) and then hit the Fastly API purge endpoint with the surrogate key when the response changes. The purge instantly replaces the cached response with a fresh response from origin.

This plugin provides three main things:

  • Instance and class methods on ActiveRecord (or Mongoid) objects for surrogate keys and purging
  • Controller helpers to set Cache-Control and Surrogate-Control response headers
  • A controller helper to set Surrogate-Key headers on responses

If you're not familiar with Fastly Surrogate Keys, you might want to check out API Caching and Fastly Surrogate Keys for a primer.

Setup

Add to your Gemfile

gem 'fastly-rails'

Configuration

For information about how to find your Fastly API Key or a Fastly Service ID, refer to our documentation.

Create an initializer for Fastly configuration

FastlyRails.configure do |c|
  c.api_key = ENV['FASTLY_API_KEY']  # Fastly api key, required
  c.max_age = 86400                  # time in seconds, optional, defaults to 2592000 (30 days)
  c.stale_while_revalidate = 86400   # time in seconds, optional, defaults to nil
  c.stale_if_error = 86400           # time in seconds, optional, defaults to nil
  c.service_id = ENV['SERVICE_ID']   # The Fastly service you will be using, required
  c.purging_enabled = !Rails.env.development? # No need to configure a client locally (AVAILABLE ONLY AS OF 0.4.0)
end

Note: purging only requires that you authenticate with your api_key. However, you can provide a user and password if you are using other endpoints in fastly-ruby that require full-auth. Also, you must provide a service_id for purges to work.

Usage

Surrogate Keys

Surrogate keys are what Fastly uses to purge groups of individual objects from our caches.

This plugin adds a few methods to generate surrogate keys automatically. table_key and record_key methods are added to any ActiveRecord::Base instance. table_key is also added to any ActiveRecord::Base class. In fact, table_key on an instance just calls table_key on the class.

We've chosen a simple surrogate key pattern by default. It is:

table_key: self.class.table_key # calls table_name on the class
record_key: "#{table_key}/#{self.id}"

e.g. If you have an ActiveRecord Model named Book.

table key: books
record key: books/1, books/2, books/3, etc...

You can easily override these methods in your models to use custom surrogate keys that may fit your specific application better:

def self.table_key
  "my_custom_table_key"
end

def record_key
  "my_custom_record_key"# Ensure these are unique for each record
end

Headers

This plugin adds a set_cache_control_headers method to ActionController. You'll need to add this in a before_filter or after_filter see note on cookies below to any controller action that you wish to edge cache (see example below). The method sets Cache-Control and Surrogate-Control HTTP Headers with a default of 30 days (remember you can configure this, see the initializer setup above).

It's up to you to set Surrogate-Key headers for objects that you want to be able to purge.

To do this use the set_surrogate_key_header method on GET actions.

class BooksController < ApplicationController
  # include this before_filter in controller endpoints that you wish to edge cache
  before_filter :set_cache_control_headers, only: [:index, :show]
  # This can be used with any customer actions. Set these headers for GETs that you want to cache
  # e.g. before_filter :set_cache_control_headers, only: [:index, :show, :my_custom_action]

  def index
    @books = Book.all
    set_surrogate_key_header 'books', @books.map(&:record_key)
  end

  def show
    @book = Book.find(params[:id])
    set_surrogate_key_header @book.record_key
  end
end

Purges

Any object that inherits from ActiveRecord will have purge_all, soft_purge_all, and table_key class methods available as well as purge, soft_purge, purge_all, and soft_purge_all instance methods.

Example usage is show below.

class BooksController < ApplicationController

  def create
    @book = Book.new(params)
    if @book.save
      @book.purge_all
      render @book
    end
  end

  def update
    @book = Book.find(params[:id])
    if @book.update(params)
      @book.purge
      render @book
    end
  end

  def delete
    @book = Book.find(params[:id])
    if @book.destroy
      @book.purge # purge the record
      @book.purge_all # purge the collection so the record is no longer there
    end
  end
end

To simplify controller methods, you could use ActiveRecord callbacks. e.g.

class Book < ActiveRecord::Base
  after_create :purge_all
  after_save :purge
  after_destroy :purge, :purge_all
  ...

end

We have left these out intentially, as they could potentially cause issues when running locally or testing. If you do use these, pay attention, as using callbacks could also inadvertently overwrite HTTP Headers like Cache-Control or Set-Cookie and cause responses to not be properly cached.

Service id

One thing to note is that currently we expect a service_id to be defined in your FastlyRails.configuration. However, we've added localized methods so that your models can override your global service_id, if you needed to operate on more than one for any reason. NOTE: As of 0.3.0, we've renamed the class-level and instance-level service_id methods to fastly_service_identifier in the active_record and mongoid mix-ins. See the CHANGELOG for a link to the Github issue.

Currently, this would require you to basically redefine fastly_service_identifier on the class level of your model:

class Book < ActiveRecord::Base
  def self.fastly_service_identifier
    'MYSERVICEID'
  end
end

Sessions, Cookies, and private data

By default, Fastly will not cache any response containing a Set-Cookie header. In general, this is beneficial because caching responses that contain sensitive data is typically not done on shared caches.

In this plugin the set_cache_control_headers method removes the Set-Cookie header from a request. In some cases, other libraries, particularily middleware, may insert or modify HTTP Headers outside the scope of where the set_cache_control_heades method is invoked in a controller action. For example, some authentication middleware will add a Set-Cookie header into requests after fastly-rails removes it.

This can cause some requests that can (and should) be cached to not be cached due to the presence of Set-Cookie.

In order to remove the Set-Cookie header in these cases, fastly-rails provides an optional piece of middleware that removes Set-Cookie when the Surrogate-Control or Surrogate-Key header is present (the Surrogate-Control header is also inserted by the set_cache_control_headers method and indicates that you want the endpoint to be cached by Fastly and do not need cookies).

fastly-rails middleware to delete Set-Cookie

To override a piece of middleware in Rails, insert custom middleware before it. Once you've identified which middleware is inserting the Set-Cookie header, add the following (in this example, ExampleMiddleware is what we are trying to override`:

# config/application.rb

  config.middleware.insert_before(
    ExampleMiddleware,
    "FastlyRails::Rack::RemoveSetCookieHeader"
  )

Example

Check out our example todo app which has a full example of fastly-rails integration in a simple rails app.

Future Work

  • Add an option to send purges in batches.

This will cut down on response delay from waiting for large amounts of purges to happen. This would primarily be geared towards write-heavy apps.

  • Your feedback

Testing

First, install all required gems:

$ appraisal install

This engine is capable of testing against multiple versions of Rails. It uses the appraisal gem. To make this happen, use the appraisal command in lieu of rake test:

$ appraisal rake test # tests against all the defined versions in the Appraisals file

$ appraisal rails-3 rake test # finds a defined version in the Appraisals file called "rails-3" and only runs tests against this version

Supported Platforms

We run tests using all combinations of the following versions of Ruby and Rails:

Ruby:

  • 1.9.3
  • 2.1.1

Rails:

  • v3.2.18
  • v4.0.5
  • v4.1.1

Other Platforms

As of v0.1.2, experimental Mongoid support was added by @joshfrench of Upworthy.

Credits

This plugin was developed by Fastly with lots of help from our friend at Hotel Tonight, Harlow Ward. Check out his blog about Cache Invalidation with Fastly Surrogate Keys which is where many of the ideas used in this plugin originated.

-- This project rocks and uses MIT-LICENSE.

More Repositories

1

pushpin

A proxy server for adding push to your API, used at the core of Fastly's Fanout service
Rust
3,654
star
2

ftw

Framework for Testing WAFs (FTW!)
Python
264
star
3

js-compute-runtime

JavaScript SDK and runtime for building Fastly Compute applications
JavaScript
199
star
4

go-fastly

A Fastly API client for Go
Go
154
star
5

Viceroy

Viceroy provides local testing for developers working with Compute.
Rust
149
star
6

cli

Build, deploy and configure Fastly services from your terminal
Go
139
star
7

fastly-magento2

Module for integrating Fastly CDN with Magento 2 installations
PHP
125
star
8

terraform-provider-fastly

Terraform Fastly provider
Go
119
star
9

Avalanche

Random, repeatable network fault injection
Python
104
star
10

fastly-exporter

A Prometheus exporter for the Fastly Real-time Analytics API
Go
98
star
11

fastly-ruby

A Fastly API client for Ruby
Ruby
91
star
12

compute-sdk-go

Go SDK for building Fastly Compute applications
Go
79
star
13

wafefficacy

Measures the effectiveness of your Web Application Firewall (WAF)
Go
76
star
14

fastly-py

A Fastly API client for Python
Python
76
star
15

sidekiq-prometheus

Public repository with Prometheus instrumentation for Sidekiq
Ruby
75
star
16

next-compute-js

Run Next.js on Fastly Compute
TypeScript
74
star
17

WordPress-Plugin

The Official Fastly WordPress Plugin
JavaScript
59
star
18

uslab

Lock-free slab allocator / freelist.
C
57
star
19

compute-starter-kit-rust-default

Default package template for Rust based Compute projects
Rust
50
star
20

go-utils

utils for go
Go
44
star
21

compute-actions

GitHub Actions for building on Fastly Compute.
JavaScript
41
star
22

insights.js

Real user monitoring of network timing signals using the Open Insights framework
TypeScript
40
star
23

compute-rust-auth

Authentication at Fastly's edge, using OAuth 2.0, OpenID Connect, and Fastly Compute.
Rust
38
star
24

waf_testbed

Chef Cookbook which provisions apache+mod_security+owasp-crs
HTML
35
star
25

fastlyctl

A CLI for managing Fastly configurations
Ruby
35
star
26

fastly2git

Create a git repository from Fastly service generated VCL
Ruby
32
star
27

token-functions

Example implementations for Fastly's token validation
Java
29
star
28

terrarium-rust-guest

The "http_guest" crate used by Fastly Labs Terrarium https://wasm.fastlylabs.com/
Rust
29
star
29

performance-observer-polyfill

🔎 Polyfill for the PerformanceObserver API
TypeScript
29
star
30

compute-js-static-publish

Static Publisher for Fastly Compute JavaScript
TypeScript
27
star
31

waflyctl

Fastly WAF CLI
Go
27
star
32

fastly-magento

Magento Extension for working with the Fastly Content Delivery Network
PHP
26
star
33

terrarium-templates

Template and example projects for Fastly Labs Terrarium https://wasm.fastlylabs.com
C
26
star
34

libvmod-urlcode

urlencode/urldecode functions vmod
C
24
star
35

fastly-php

A Fastly API client for PHP
PHP
24
star
36

expressly

Express style router for Fastly Compute
TypeScript
24
star
37

compute-starter-kit-rust-static-content

Static content starter kit for Rust based Fastly Compute projects. Speed up your websites with a Compute application serving content from a static bucket, redirects, security and performance headers, and a 404 page.
Rust
24
star
38

remix-compute-js

Remix for Fastly Compute JavaScript
TypeScript
22
star
39

log4j_interpreter

A Rust library for evaluating log4j substitution queries in order to determine whether or not malicious queries may exist.
Rust
22
star
40

compute-starter-kit-javascript-default

Default package template for JavaScript based Fastly Compute projects
JavaScript
22
star
41

vcl-json-generate

A VCL module that allows you to generate JSON dynamically on the edge
VCL
21
star
42

compute-starter-kit-assemblyscript-default

Default package template for AssemblyScript based Fastly Compute projects
TypeScript
20
star
43

fanout-compute-js-demo

Fanout Fastly Compute JavaScript demo
TypeScript
17
star
44

cd-with-terraform

Practical exercises for InfoQ "Continuous deployment with Terraform" workshop
HCL
16
star
45

fastly-perl

A Fastly API client for Perl
Perl
16
star
46

fastly-rust

A Rust Fastly API client library.
Rust
16
star
47

mruby-optparse

Port of Ruby's OptionParser to mruby
Ruby
16
star
48

http-compute-js

Node.js-compatible request and response objects
TypeScript
16
star
49

compute-js-opentelemetry

An implementation of OpenTelemetry for Fastly Compute
TypeScript
15
star
50

compute-at-edge-abi

Interface definitions for the Compute@Edge platform in witx.
Rust
15
star
51

blockbuster

VCR cassette manager
Ruby
13
star
52

demo-fiddle-ci

Using Fastly Fiddle to enable CI testing of Fastly services
JavaScript
13
star
53

secretd

Secret storage server
Go
12
star
54

heroku-fastly

Heroku CLI plugin for Fastly
JavaScript
10
star
55

go-mtr

go wrapped mtr --raw
Go
10
star
56

security-use-cases

Placeholder for security related use cases and demos
HCL
10
star
57

fastly-js

A Fastly API client for JavaScript
JavaScript
10
star
58

terrctl

A command-line client for Fastly Terrarium. https://wasm.fastlylabs.com
Go
10
star
59

compute-starter-kit-javascript-openapi-validation

OpenAPI Validation Starter Kit for Fastly Compute (JavaScript)
JavaScript
10
star
60

vscode-fastly-vcl

A Visual Studio Code extension which adds syntax highlighting for Fastly Varnish Configuration Language (VCL) files.
TypeScript
10
star
61

http_connection_monitor

Monitors your outbound HTTP requests for number of requests made over a persistent connection.
Ruby
9
star
62

fastly-test-blog

Test application for learning Fastly's UI
Ruby
9
star
63

uap-vcl

uap-vcl is a VCL module which parses a User-Agent string
VCL
8
star
64

sigsci-splunk-app

Splunk app for Fastly (Signal Sciences)
Python
8
star
65

librip

Librip is a minimal-overhead API for instruction-level tracing in highly concurrent software.
Python
8
star
66

fastly_nsq

Public repository with a convenience adapter & testing classes for apps talking to NSQ
Ruby
8
star
67

altitude-nyc-abcd-workshop

Practical exercises for "ABCD: Always be continuously deploying" workshop at Altitude NYC 2017
HCL
7
star
68

dnstap-utils

dnstap utilities implemented in Rust
Rust
7
star
69

compute-starter-kit-typescript

A simple Fastly starter kit for Typescript
TypeScript
6
star
70

ember-anti-clickjacking

Anti-Clickjacking in Ember
JavaScript
6
star
71

compute-starter-kit-rust-beacon-termination

Beacon Termination package template for Rust based Fastly Compute projects.
Rust
6
star
72

Raikkonen

Räikkönen tests races.
C
6
star
73

compute-hibp-filter

Fastly Compute enrichment to detect compromised passwords
Go
6
star
74

js-compute-testing

Write JavaScript tests from Node.js, against a local or remote Fastly Compute application
TypeScript
6
star
75

diff-service

An experiment in powering Edge diff functionality from Google Cloud Functions
JavaScript
6
star
76

jlog-go

Go bindings for jlog
Go
6
star
77

compute-ll-hls

Fastly Compute application for LL-HLS playlist manipulation.
Rust
6
star
78

vmdebootstrap

wrapper around debootstrap to create virtual machine disk images
Python
6
star
79

compute-starter-kit-rust-empty

Empty package template for Rust based Fastly Compute projects
Rust
5
star
80

compute-starter-kit-go-default

Default package template for Go based Fastly Compute projects
Go
5
star
81

altitude-ci-cd-workshop

Practical exercises for "Building a continuous deployment pipeline" workshop at Altitude 2017
HCL
5
star
82

fastly-blocklist

Configure request blocking for a Fastly service.
Python
5
star
83

compute-starter-kit-javascript-queue

Queuing package template for JavaScript based Fastly Compute projects. Park your users in a virtual queue to reduce the demand on your origins during peak times.
JavaScript
5
star
84

serve-vercel-build-output

A runtime environment that executes output that targets the Vercel Build Output API on Fastly Compute
TypeScript
5
star
85

edgeml-recommender

POC: similarity search recommendation engine at the edge using only Fastly Compute & Rust
Rust
4
star
86

compute-starter-kit-rust-websockets

WebSockets starter kit for Fastly Compute (Rust)
Rust
4
star
87

altitude-LON-logging-workshop

Fiddle links & exercises for "Building an internal analytics platform with real-time logs" workshop at Altitude LON 2019
4
star
88

irc2slack

Python
4
star
89

Varnish-API

Perl extension for accessing varnish stats and logs
C
4
star
90

security-solutions-visualization-waf-bigquery-looker

4
star
91

compute-starter-kit-javascript-empty

Empty package template for JavaScript based Fastly Compute projects
JavaScript
4
star
92

waf-simulator-automation

Example of automating waf tests with Fastly's waf simulator
Go
4
star
93

url-shortener

A tool makes your message easier to read
Rust
4
star
94

fastly-lem

Automate the deployment of Live Event Monitoring
Go
3
star
95

compute-js-auth

OAuth 2.0 implementation for Fastly Compute, in JavaScript
JavaScript
3
star
96

wasm-workshop-altitude-ldn-2019

Workshop materials for the "WebAssembly outside the web" workshop
Rust
3
star
97

compute-rust-sentry

Send error reports from Rust Fastly Compute services to Sentry.
Rust
3
star
98

compute-starter-kit-javascript-expressly

A lightweight starter kit for Fastly Compute, demonstrating the expressly framework.
JavaScript
3
star
99

fastly-template-rust-nel

Package template for a Rust based Network Error Logging Fastly Compute service
Rust
3
star
100

next-compute-js-server

Implementation of Next.js Server class for Fastly Compute JavaScript
TypeScript
3
star