Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sfewfsaf
spring-boot-examples
提交
21f0073f
S
spring-boot-examples
项目概览
sfewfsaf
/
spring-boot-examples
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-boot-examples
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
21f0073f
编写于
3月 06, 2019
作者:
微笑很纯洁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add redis examples
上级
ec86179e
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
320 addition
and
0 deletion
+320
-0
spring-boot-redis/pom.xml
spring-boot-redis/pom.xml
+60
-0
spring-boot-redis/src/main/java/com/neo/RedisApplication.java
...ng-boot-redis/src/main/java/com/neo/RedisApplication.java
+12
-0
spring-boot-redis/src/main/java/com/neo/config/RedisConfig.java
...-boot-redis/src/main/java/com/neo/config/RedisConfig.java
+34
-0
spring-boot-redis/src/main/java/com/neo/config/SessionConfig.java
...oot-redis/src/main/java/com/neo/config/SessionConfig.java
+9
-0
spring-boot-redis/src/main/java/com/neo/model/User.java
spring-boot-redis/src/main/java/com/neo/model/User.java
+88
-0
spring-boot-redis/src/main/java/com/neo/web/UserController.java
...-boot-redis/src/main/java/com/neo/web/UserController.java
+33
-0
spring-boot-redis/src/main/resources/application.properties
spring-boot-redis/src/main/resources/application.properties
+17
-0
spring-boot-redis/src/test/java/com/neo/RedisApplicationTests.java
...ot-redis/src/test/java/com/neo/RedisApplicationTests.java
+18
-0
spring-boot-redis/src/test/java/com/neo/TestRedis.java
spring-boot-redis/src/test/java/com/neo/TestRedis.java
+49
-0
未找到文件。
spring-boot-redis/pom.xml
0 → 100644
浏览文件 @
21f0073f
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.neo
</groupId>
<artifactId>
spring-boot-redis
</artifactId>
<version>
1.0.0-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
spring-boot-redis
</name>
<description>
Demo project for Spring Boot
</description>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
2.1.0.RELEASE
</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<java.version>
1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-pool2
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.session
</groupId>
<artifactId>
spring-session-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-redis/src/main/java/com/neo/RedisApplication.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
RedisApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
RedisApplication
.
class
,
args
);
}
}
spring-boot-redis/src/main/java/com/neo/config/RedisConfig.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo.config
;
import
java.lang.reflect.Method
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.annotation.CachingConfigurerSupport
;
import
org.springframework.cache.annotation.EnableCaching
;
import
org.springframework.cache.interceptor.KeyGenerator
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.redis.cache.RedisCacheManager
;
import
org.springframework.data.redis.core.RedisTemplate
;
@Configuration
@EnableCaching
public
class
RedisConfig
extends
CachingConfigurerSupport
{
@Bean
public
KeyGenerator
keyGenerator
()
{
return
new
KeyGenerator
()
{
@Override
public
Object
generate
(
Object
target
,
Method
method
,
Object
...
params
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
target
.
getClass
().
getName
());
sb
.
append
(
method
.
getName
());
for
(
Object
obj
:
params
)
{
sb
.
append
(
obj
.
toString
());
}
return
sb
.
toString
();
}
};
}
}
\ No newline at end of file
spring-boot-redis/src/main/java/com/neo/config/SessionConfig.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo.config
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession
;
@Configuration
@EnableRedisHttpSession
(
maxInactiveIntervalInSeconds
=
86400
*
30
)
public
class
SessionConfig
{
}
\ No newline at end of file
spring-boot-redis/src/main/java/com/neo/model/User.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo.model
;
import
java.io.Serializable
;
public
class
User
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
private
Long
id
;
private
String
userName
;
private
String
password
;
private
String
email
;
private
String
nickname
;
private
String
regTime
;
public
User
()
{
super
();
}
public
User
(
String
email
,
String
nickname
,
String
password
,
String
userName
,
String
regTime
)
{
super
();
this
.
email
=
email
;
this
.
nickname
=
nickname
;
this
.
password
=
password
;
this
.
userName
=
userName
;
this
.
regTime
=
regTime
;
}
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
String
getUserName
()
{
return
userName
;
}
public
void
setUserName
(
String
userName
)
{
this
.
userName
=
userName
;
}
public
String
getPassword
()
{
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
String
getEmail
()
{
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getNickname
()
{
return
nickname
;
}
public
void
setNickname
(
String
nickname
)
{
this
.
nickname
=
nickname
;
}
public
String
getRegTime
()
{
return
regTime
;
}
public
void
setRegTime
(
String
regTime
)
{
this
.
regTime
=
regTime
;
}
@Override
public
String
toString
()
{
return
"User{"
+
"id="
+
id
+
", userName='"
+
userName
+
'\''
+
", password='"
+
password
+
'\''
+
", email='"
+
email
+
'\''
+
", nickname='"
+
nickname
+
'\''
+
", regTime='"
+
regTime
+
'\''
+
'}'
;
}
}
\ No newline at end of file
spring-boot-redis/src/main/java/com/neo/web/UserController.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo.web
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.neo.model.User
;
import
javax.servlet.http.HttpSession
;
import
java.util.UUID
;
@RestController
public
class
UserController
{
@RequestMapping
(
"/getUser"
)
@Cacheable
(
value
=
"user-key"
)
public
User
getUser
()
{
User
user
=
new
User
(
"aa@126.com"
,
"aa"
,
"aa123456"
,
"aa"
,
"123"
);
System
.
out
.
println
(
"若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"
);
return
user
;
}
@RequestMapping
(
"/uid"
)
String
uid
(
HttpSession
session
)
{
UUID
uid
=
(
UUID
)
session
.
getAttribute
(
"uid"
);
if
(
uid
==
null
)
{
uid
=
UUID
.
randomUUID
();
}
session
.
setAttribute
(
"uid"
,
uid
);
return
session
.
getId
();
}
}
\ No newline at end of file
spring-boot-redis/src/main/resources/application.properties
0 → 100644
浏览文件 @
21f0073f
# REDIS
# Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09
spring.redis.database
=
0
# Redis\u670D\u52A1\u5668\u5730\u5740
spring.redis.host
=
localhost
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
spring.redis.port
=
6379
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
spring.redis.password
=
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 8
spring.redis.lettuce.pool.max-active
=
8
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 -1
spring.redis.lettuce.pool.max-wait
=
-1
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 8
spring.redis.lettuce.pool.max-idle
=
8
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 0
spring.redis.lettuce.pool.min-idle
=
0
spring-boot-redis/src/test/java/com/neo/RedisApplicationTests.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
RedisApplicationTests
{
@Test
public
void
contextLoads
()
{
System
.
out
.
println
(
"hello web"
);
}
}
spring-boot-redis/src/test/java/com/neo/TestRedis.java
0 → 100644
浏览文件 @
21f0073f
package
com.neo
;
import
com.neo.model.User
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.ValueOperations
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.util.concurrent.TimeUnit
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
TestRedis
{
@Autowired
private
StringRedisTemplate
stringRedisTemplate
;
@Autowired
private
RedisTemplate
redisTemplate
;
@Test
public
void
test
()
throws
Exception
{
stringRedisTemplate
.
opsForValue
().
set
(
"aaa"
,
"111"
);
Assert
.
assertEquals
(
"111"
,
stringRedisTemplate
.
opsForValue
().
get
(
"aaa"
));
}
@Test
public
void
testObj
()
throws
Exception
{
User
user
=
new
User
(
"aa@126.com"
,
"aa"
,
"aa123456"
,
"aa"
,
"123"
);
ValueOperations
<
String
,
User
>
operations
=
redisTemplate
.
opsForValue
();
operations
.
set
(
"com.neox"
,
user
);
operations
.
set
(
"com.neo.f"
,
user
,
1
,
TimeUnit
.
SECONDS
);
Thread
.
sleep
(
1000
);
//redisTemplate.delete("com.neo.f");
boolean
exists
=
redisTemplate
.
hasKey
(
"com.neo.f"
);
if
(
exists
){
System
.
out
.
println
(
"exists is true"
);
}
else
{
System
.
out
.
println
(
"exists is false"
);
}
// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录