GithubHelp home page GithubHelp logo

geo_web's Introduction

Web

Web is a great way to automatically record HTTP responses in a cache so you don't overwhelm an external service. You can also share the responses between multiple machines and multiple adapters (like typhoeus and net/http) seamlessly.

Web.register :get, /google\.com/

By doing that, any request is cached and will return as if it hit the web service directly. By default, the caching is to memory, but there are also adapters for caching via Memcached, Redis, or a custom adapter. (see below)

Web.register :any, /google\.com/
Net::HTTP.get_print URI.parse 'http://google.com' # from source
Net::HTTP.get_print URI.parse 'http://google.com' # from cache!
Typhoeus::Request.get 'http://google.com' # from cache!

Auto-expiry

You can also do

Web.register :get, /google\.com/, :expire => 2

To automatically expire requests to google.com every 2 seconds


Different Caches

There are multiple cache classes, and you can add your own. MemoryCache caching is the default, but if you want to change it you can do something like:

Web.cache = Web::MemcachedCache.new

Existing Cache Classes

  • MemoryCache - Caches to memory directly
  • RedisCache - A cache backed by Redis
  • MemcachedCache - A cache backed by Memcached via Dalli

Custom Cache Classes

Custom caches are expected to respond to get(key) and set(key, value, expiry_s). lib/web/cache/redis_cache.rb is a great example implementation.


Adapters

There is currently support for the following adapters:

  • typhoeus
  • net/http

It's easy to write new adapters, and the typhoeus adapter is a great example. Give it a read at lib/web/ext/typhoeus.rb and contribute one for another library, like curb or patron.

Seeing if a response is cached

Response objects from the adapters all have an added method called cached? that will return a boolean indicating whether or not the source of the response was the cache.

Installation

gem install web

Author

John Crepezzi ([email protected])

geo_web's People

Contributors

seejohnrun avatar

Stargazers

 avatar Marc Bee avatar

Watchers

Marc Bee avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.