27
27
import apijson .orm .SQLConfig ;
28
28
import com .alibaba .druid .pool .DruidDataSource ;
29
29
import com .alibaba .fastjson .JSONObject ;
30
- // import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
31
- // import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
32
- // import org.springframework.data.redis.core.RedisTemplate;
33
- // import org.springframework.data.redis.serializer.GenericToStringSerializer;
34
- // import org.springframework.data.redis.serializer.StringRedisSerializer;
30
+ import org .springframework .data .redis .connection .RedisStandaloneConfiguration ;
31
+ import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
32
+ import org .springframework .data .redis .core .RedisTemplate ;
33
+ import org .springframework .data .redis .serializer .GenericToStringSerializer ;
34
+ import org .springframework .data .redis .serializer .StringRedisSerializer ;
35
35
36
36
import javax .sql .DataSource ;
37
37
import java .io .Serializable ;
@@ -58,66 +58,66 @@ public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
58
58
public static final String TAG = "DemoSQLExecutor" ;
59
59
60
60
// Redis 缓存 <<<<<<<<<<<<<<<<<<<<<<<
61
- // public static final RedisTemplate<String, String> REDIS_TEMPLATE;
62
- // static {
63
- // REDIS_TEMPLATE = new RedisTemplate<>();
64
- // try {
65
- // REDIS_TEMPLATE.setConnectionFactory(new JedisConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379)));
66
- // REDIS_TEMPLATE.setKeySerializer(new StringRedisSerializer());
67
- // REDIS_TEMPLATE.setHashValueSerializer(new GenericToStringSerializer<>(Serializable.class));
68
- // REDIS_TEMPLATE.setValueSerializer(new GenericToStringSerializer<>(Serializable.class));
69
- // // REDIS_TEMPLATE.setValueSerializer(new FastJsonRedisSerializer<List<JSONObject>>(List.class));
70
- // REDIS_TEMPLATE.afterPropertiesSet();
71
- // } catch (Throwable e) {
72
- // e.printStackTrace();
73
- // }
74
- // }
75
- //
76
- // // 可重写以下方法,支持 Redis 等单机全局缓存或分布式缓存
77
- // @Override
78
- // public List<JSONObject> getCache(String sql, SQLConfig<Long> config) {
79
- // List<JSONObject> list = super.getCache(sql, config);
80
- // if (list == null) {
81
- // try {
82
- // list = JSON.parseArray(REDIS_TEMPLATE.opsForValue().get(sql), JSONObject.class);
83
- // } catch (Throwable e) {
84
- // e.printStackTrace();
85
- // }
86
- // }
87
- // return list;
88
- // }
89
- //
90
- // @Override
91
- // public synchronized void putCache(String sql, List<JSONObject> list, SQLConfig<Long> config) {
92
- // super.putCache(sql, list, config);
93
- //
94
- // String table = config != null && config.isMain() ? config.getTable() : null;
95
- // if (table != null && ! DemoSQLConfig.CONFIG_TABLE_LIST.contains(table)) {
96
- // try {
97
- // if (config.isExplain() || RequestMethod.isHeadMethod(config.getMethod(), true)) {
98
- // REDIS_TEMPLATE.opsForValue().set(sql, JSON.toJSONString(list), 10 * 60, TimeUnit.SECONDS);
99
- // } else {
100
- // REDIS_TEMPLATE.opsForValue().set(sql, JSON.toJSONString(list), USER_.equals(table) || PRIVACY_.equals(table) ? 10 * 60 : 60, TimeUnit.SECONDS);
101
- // }
102
- // } catch (Throwable e) {
103
- // e.printStackTrace();
104
- // }
105
- // }
106
- // }
107
- //
108
- // @Override
109
- // public synchronized void removeCache(String sql, SQLConfig<Long> config) {
110
- // super.removeCache(sql, config);
111
- // try {
112
- // if (config.getMethod() == RequestMethod.DELETE) { // 避免缓存击穿
113
- // REDIS_TEMPLATE.expire(sql, 60, TimeUnit.SECONDS);
114
- // } else {
115
- // REDIS_TEMPLATE.delete(sql);
116
- // }
117
- // } catch (Throwable e) {
118
- // e.printStackTrace();
119
- // }
120
- // }
61
+ public static final RedisTemplate <String , String > REDIS_TEMPLATE ;
62
+ static {
63
+ REDIS_TEMPLATE = new RedisTemplate <>();
64
+ try {
65
+ REDIS_TEMPLATE .setConnectionFactory (new JedisConnectionFactory (new RedisStandaloneConfiguration ("127.0.0.1" , 6379 )));
66
+ REDIS_TEMPLATE .setKeySerializer (new StringRedisSerializer ());
67
+ REDIS_TEMPLATE .setHashValueSerializer (new GenericToStringSerializer <>(Serializable .class ));
68
+ REDIS_TEMPLATE .setValueSerializer (new GenericToStringSerializer <>(Serializable .class ));
69
+ // REDIS_TEMPLATE.setValueSerializer(new FastJsonRedisSerializer<List<JSONObject>>(List.class));
70
+ REDIS_TEMPLATE .afterPropertiesSet ();
71
+ } catch (Throwable e ) {
72
+ e .printStackTrace ();
73
+ }
74
+ }
75
+
76
+ // 可重写以下方法,支持 Redis 等单机全局缓存或分布式缓存
77
+ @ Override
78
+ public List <JSONObject > getCache (String sql , SQLConfig <Long > config ) {
79
+ List <JSONObject > list = super .getCache (sql , config );
80
+ if (list == null ) {
81
+ try {
82
+ list = JSON .parseArray (REDIS_TEMPLATE .opsForValue ().get (sql ), JSONObject .class );
83
+ } catch (Throwable e ) {
84
+ e .printStackTrace ();
85
+ }
86
+ }
87
+ return list ;
88
+ }
89
+
90
+ @ Override
91
+ public synchronized void putCache (String sql , List <JSONObject > list , SQLConfig <Long > config ) {
92
+ super .putCache (sql , list , config );
93
+
94
+ String table = config != null && config .isMain () ? config .getTable () : null ;
95
+ if (table != null && ! DemoSQLConfig .CONFIG_TABLE_LIST .contains (table )) {
96
+ try {
97
+ if (config .isExplain () || RequestMethod .isHeadMethod (config .getMethod (), true )) {
98
+ REDIS_TEMPLATE .opsForValue ().set (sql , JSON .toJSONString (list ), 10 * 60 , TimeUnit .SECONDS );
99
+ } else {
100
+ REDIS_TEMPLATE .opsForValue ().set (sql , JSON .toJSONString (list ), USER_ .equals (table ) || PRIVACY_ .equals (table ) ? 10 * 60 : 60 , TimeUnit .SECONDS );
101
+ }
102
+ } catch (Throwable e ) {
103
+ e .printStackTrace ();
104
+ }
105
+ }
106
+ }
107
+
108
+ @ Override
109
+ public synchronized void removeCache (String sql , SQLConfig <Long > config ) {
110
+ super .removeCache (sql , config );
111
+ try {
112
+ if (config .getMethod () == RequestMethod .DELETE ) { // 避免缓存击穿
113
+ REDIS_TEMPLATE .expire (sql , 60 , TimeUnit .SECONDS );
114
+ } else {
115
+ REDIS_TEMPLATE .delete (sql );
116
+ }
117
+ } catch (Throwable e ) {
118
+ e .printStackTrace ();
119
+ }
120
+ }
121
121
122
122
// Redis 缓存 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
123
123
0 commit comments