Skip to content

Commit a162089

Browse files
committed
修改redis-store中的http缓存方式
1 parent fb4d904 commit a162089

7 files changed

Lines changed: 40 additions & 13 deletions

File tree

app/views/static_pages/_static_page.json.jbuilder

Lines changed: 0 additions & 2 deletions
This file was deleted.

app/views/static_pages/index.json.jbuilder

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/static_pages/show.json.jbuilder

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# json.array! @users, partial: 'users/user', as: :user
2-
json.content format_content(@message.content)
1+
# binding.pry
2+
json.array! @users, partial: 'users/user', as: :user
3+
# json.content format_content(@message.content)

config/environments/development.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
config.consider_all_requests_local = true
1414

1515
# Enable/disable caching. By default caching is disabled.
16-
# Run rails dev:cache to toggle caching.
16+
# 运行命令: rails dev:cache 可以开启文件缓存。
1717
if Rails.root.join('tmp', 'caching-dev.txt').exist?
1818
config.action_controller.perform_caching = true
1919

@@ -24,13 +24,19 @@
2424
else
2525
config.action_controller.perform_caching = true
2626
# config.cache_store = :null_store
27-
config.cache_store = :redis_cache_store, CACHE_REDIS_CONFIG.merge({namespace: 'cache', compress: true, expires_in: 10.minutes})
27+
# config.cache_store = :redis_cache_store, CACHE_REDIS_CONFIG.merge({namespace: 'cache', compress: true, expires_in: 10.minutes})
28+
config.cache_store = :redis_store, "redis://localhost:6379/2/cache", { expires_in: 90.minutes }
29+
# config.cache_store = :redis_cache_store, {driver: :hiredis, namespace: 'cache', compress: true,
30+
# timeout: 1, url: "redis://127.0.0.1:6379/2"}
2831
end
2932

3033
# 配置http缓存
34+
redis_password = CACHE_REDIS_CONFIG[:password].present? ? ":#{CACHE_REDIS_CONFIG[:password]}@": ""
35+
metastore_redis_config = "redis://#{redis_password}#{CACHE_REDIS_CONFIG[:host]}:#{CACHE_REDIS_CONFIG[:port]}/#{CACHE_REDIS_CONFIG[:db]}/metastore"
36+
entitystore_redis_config = "redis://#{redis_password}#{CACHE_REDIS_CONFIG[:host]}:#{CACHE_REDIS_CONFIG[:port]}/#{CACHE_REDIS_CONFIG[:db]}/entitystore"
3137
config.action_dispatch.rack_cache = {
32-
metastore: CACHE_REDIS_CONFIG.merge({namespace: 'metastore'}),
33-
entitystore: CACHE_REDIS_CONFIG.merge({namespace: 'entitystore'})
38+
metastore: metastore_redis_config,
39+
entitystore: entitystore_redis_config
3440
}
3541

3642
# Store uploaded files on the local file system (see config/storage.yml for options)

config/environments/production.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@
7676
config.cache_store = :redis_cache_store, CACHE_REDIS_CONFIG.merge({namespace: 'cache', compress: true})
7777

7878
# 配置http缓存
79+
redis_password = CACHE_REDIS_CONFIG[:password].present? ? ":#{CACHE_REDIS_CONFIG[:password]}@": ""
80+
metastore_redis_config = "redis://#{redis_password}#{CACHE_REDIS_CONFIG[:host]}:#{CACHE_REDIS_CONFIG[:port]}/#{CACHE_REDIS_CONFIG[:db]}/metastore"
81+
entitystore_redis_config = "redis://#{redis_password}#{CACHE_REDIS_CONFIG[:host]}:#{CACHE_REDIS_CONFIG[:port]}/#{CACHE_REDIS_CONFIG[:db]}/entitystore"
7982
config.action_dispatch.rack_cache = {
80-
metastore: CACHE_REDIS_CONFIG.merge({namespace: 'metastore'}),
81-
entitystore: CACHE_REDIS_CONFIG.merge({namespace: 'entitystore'})
83+
metastore: metastore_redis_config,
84+
entitystore: entitystore_redis_config
8285
}
8386

8487
# Use a real queuing backend for Active Job (and separate queues per environment)

docs/Jbuilder.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ create app/views/tests/_test.json.jbuilder
2020
```ruby
2121
rails g jbuilder Test name:string
2222
```
23-
就可以在_test.json.jbuilder文件中看到有name字段的展现了。
23+
就可以在_test.json.jbuilder文件中看到有name字段的展现了。
24+
25+
请求访问数据,可以在路由后面加上.json,例如访问
26+
```html
27+
http://localhost:3000/users.json
28+
http://localhost:3000/users/1.json
29+
```
30+
2431
定义数组并从对象中提取属性放入该数组中:
2532
```ruby
2633
# @people = People.all
@@ -32,7 +39,21 @@ json.array! @users do |item|
3239
json.name item.name
3340
end
3441
# => [{"id":1, "name":"wangyun"}]
42+
43+
# 提取方法对象里面的属性
44+
json.extract! @users, :id, :name, :email, :age, :sex, :created_at, :updated_at
45+
# 输出的数据
46+
{
47+
"id": 1,
48+
"name": "wangyun",
49+
"email": "xxx@qq.com",
50+
"age": "18",
51+
"sex": false,
52+
"created_at": "2019-04-24T02:02:30.000Z",
53+
"updated_at": "2019-04-24T02:07:49.000Z",
54+
}
3555
```
56+
3657
动态定义属性和结构名:
3758
```ruby
3859
json.set! :author do

0 commit comments

Comments
 (0)