https://github.com/janjiss/sha_cache
Saving your CPU resources since 2013
https://github.com/janjiss/sha_cache
Last synced: 3 months ago
JSON representation
Saving your CPU resources since 2013
- Host: GitHub
- URL: https://github.com/janjiss/sha_cache
- Owner: janjiss
- License: mit
- Created: 2013-12-08T20:20:39.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-09T09:11:34.000Z (over 11 years ago)
- Last Synced: 2025-01-10T17:20:18.477Z (5 months ago)
- Language: Ruby
- Homepage:
- Size: 147 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# ShaCache
If you want to avoid heavy data processing for the same responses from 3rd party API? ShaCache allows you to do it by caching sha of responses. After that you can skip data processing if data has not changed.
# Note
Still to test this working
## The problem
* You send request to server /user/profile.json and get following response:
```json
{"username": "janis"}
```* You do some processing based on that data
* You make request second time and response stays the same:```json
{"username": "janis"}
```* You do the processing again.
## The solution
* You send request to server /user/profile.json and get following response:```json
{"username": "janis"}
```* You cache sha for response body with unique key that suits you (It can be user.id or something else)
```ruby
SchCache::Client.write_data(user.id, "{\"username\": \"janis\"}")
```* Next time you check if SHA is the same for that response body and, if it is, skip your processing
```ruby
if ShaCache::Client.has_data_with_key?(user.id, "{\"username\": \"janis\"}")
skip_data_processing
else
do_data_processing
end
```# Configuration
First of all, you will have to configure adapter (support is only available for Redis right now) but you can write your own:```ruby
ShaCache::Config.config do |c|
c.adapter = ShaCache::Adapter::Redis
end
```After that you will need to pass redis instance you are working with:
```ruby
@redis_obj = Redis.new
ShaCahce::Adapters::Redis.config do |c|
c.redis_obj = @redis_obj
end
```## Installation
Add this line to your application's Gemfile:
gem 'sha_cache'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sha_cache
## 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