配置html
新建spring boot項目增長redis配置java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
在須要緩存的地方增長redis緩存redis
package com.example.sharding.service; import com.alibaba.fastjson.JSON; import com.example.sharding.entity.Order; import com.example.sharding.mapper.OrderMapper; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.List; @Service @Transactional public class OrderService { @Autowired private OrderMapper orderMapper; @Autowired private RedisTemplate redisTemplate; public Object findAll() throws IOException { List<Order> orders = orderMapper.selectAll(); ObjectMapper mapper = new ObjectMapper(); redisTemplate.opsForValue().set("orders",mapper.writeValueAsString(orders)); return orders; } public void save(Order order) { orderMapper.insert(order); redisTemplate.opsForHash().put("order",order.getOrderId().toString(), JSON.parseObject(JSON.toJSONString(order)).toJSONString()); } public void deleteAll() { orderMapper.deleteAll(); redisTemplate.delete("order"); redisTemplate.delete("orders"); } }
修改example.confspring
server { listen 80; server_name _; location = /order { default_type 'text/html'; lua_code_cache off; content_by_lua_file C:\work\openresty-1.13.6.1-win32\example\lua/test_redis_basic.lua; } location /lua { default_type 'text/html'; lua_code_cache off; content_by_lua_file C:\work\openresty-1.13.6.1-win32\example\lua/test.lua; } # 其餘請求回源到tomcat location / { proxy_pass http://tomcat/; } } # 後端服務地址 upstream tomcat { server 127.0.0.1:8080 max_fails=5 fail_timeout=10s weight=1; keepalive 100; }
修改test_redis_basic.lua文件json
local function close_redis(red) if not red then return end local ok, err = red:close() if not ok then ngx.say("close redis error : ", err) end end local function read_order() local resp = ngx.location.capture("/order/query", { method = ngx.HTTP_GET }) if not resp then return end if resp.status ~= 200 then return end return resp.body end local redis = require("resty.redis") local cjson = require("cjson") -- 引入json模塊 --建立實例 local red = redis:new() --設置超時(毫秒) red:set_timeout(1000) --創建鏈接 local ip = "127.0.0.1" local port = 6379 local ok, err = red:connect(ip, port) if not ok then ngx.say("connect to redis error : ", err) return close_redis(red) end local res, err = red:auth("shiyuesoft") if not res then ngx.say("failed to authenticate: ", err) return end --調用API獲取數據 local resp, err = red:get("orders") if not resp then ngx.say("get msg error : ", err) return close_redis(red) end --獲得的數據爲空處理 if resp == ngx.null then resp = read_order() --回源到tomcat去查詢 end ngx.say(cjson.decode(resp)) --對返回的數據轉換成json對象 close_redis(red)
先請求deleteAll刪除全部緩存後端
新增order數據緩存
訪問/order地址tomcat
第一次因爲沒有緩存,會進入接口查詢bash
後面繼續刷新頁面,不會再進入後端接口了app