Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joker1007/redis-cacheable
It is concern style redis caching helper. It makes very easy to cache object.
https://github.com/joker1007/redis-cacheable
Last synced: 2 days ago
JSON representation
It is concern style redis caching helper. It makes very easy to cache object.
- Host: GitHub
- URL: https://github.com/joker1007/redis-cacheable
- Owner: joker1007
- License: mit
- Created: 2013-11-05T18:03:24.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-04-07T16:17:03.000Z (over 10 years ago)
- Last Synced: 2024-10-18T14:59:21.492Z (21 days ago)
- Language: Ruby
- Homepage:
- Size: 284 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# redis-cacheable
[![Gem Version](https://badge.fury.io/rb/redis-cacheable.png)](http://badge.fury.io/rb/redis-cacheable)
[![Build Status](https://travis-ci.org/joker1007/redis-cacheable.png?branch=master)](https://travis-ci.org/joker1007/redis-cacheable)
[![Coverage Status](https://coveralls.io/repos/joker1007/redis-cacheable/badge.png?branch=master)](https://coveralls.io/r/joker1007/redis-cacheable?branch=master)
[![Code Climate](https://codeclimate.com/github/joker1007/redis-cacheable.png)](https://codeclimate.com/github/joker1007/redis-cacheable)It is concern style redis caching helper.
It makes very easy to cache object.## Installation
Add this line to your application's Gemfile:
gem 'redis-cacheable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install redis-cacheable
## Usage
### Plain Object
```ruby
class MyObject
include RedisCacheableattr_reader :id, :name
redis_key :id # optional (default: :id)
redis_attrs :id, :name # method namesdef initialize(attributes)
@id = attributes[:id]
@name = attributes[:name]
end
endclass ProcObject < MyObject
redis_attrs ->(obj) { obj.id * 10 } # can give proc
end
``````ruby
my_object = MyObject.new(id: 1, name: "my_object")
my_object.cache_to_redis # KEY = my_object.id, VALUE = MultiJson.dump({"id" => my_object.id, "name" => my_object.name})MyObject.find_from_redis(1) # => {"id" => 1, "name" => "my_object"}
proc_object = ProcObject.new(id: 1, name: "proc_object")
proc_object.cache_to_redis # different namespace with MyObject
ProcObject.find_from_redis(1) # => 10## set expiration
my_object.expire_redis(600)
my_object.expireat_redis(Time.local(2014 ,4, 8, 12, 0).to_i)## delete from redis
my_object.del_from_redis
```### ActiveRecord
If you use ActiveRecord, can include more specialized module.
```ruby
# id: integer
# name: string
# rate: floatclass MyRecord < ActiveRecord::Base
include RedisCacheable::ActiveRecord
redis_attrs :id, :name, :rate # method names
end
``````ruby
record = MyRecord.create(name: "my_record", rate: 4.5)
record.cache_to_redis
MyRecord.find_from_redis(record.id) #=> #MyRecord.cache_all # cache all records
```## Configuration
```ruby
RedisCacheable::Configuration.configure do |config|
config.host = "10.0.0.1" # redis host (default: "localhost")
config.port = 6380 # redis port (default: 6379)
config.driver = :hiredis # redis port (default: :ruby)
config.namespace_prefix = "myapp" # see below.
config.pool_size = 10 # connection pool size (default: 5)
config.timeout = 10 # timeout seconds (default: 5)
end
````config.namespace_prefix` is useful, If multi application use same redis.
```ruby
RedisCacheable.configure do |config|
config.namespace_prefix = "myapp" # see below.
endclass MyObject
include RedisCacheable# ...
end# MyObject redis namespace is "myapp_my_object"
```## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request