- 浏览: 2566616 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Redis(4)Learn from Example
1. Domain Layer
Map the Java classes of our domain to Hash Type in redis.
User id =1, username = karl, password = 111111, role = 1
redis>hmset user:1 id 1 username karl password 111111 role 1
redis>hgetall user:1
If you want to validate your redis command, you can try it here: http://try.redis-db.com/
So there is no annotation in domain layer. So my domain will be just the User and Role
public class User {
private String id;
private String firstName;
private String lastName;
private String username;
private String password;
private Role role;
...snip... getter and setter
public class Role {
private String id;
private Integer role;
...snip... getter and setter
2. Service Layer
package com.sillycat.easynosql.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import com.sillycat.easynosql.model.Role;
import com.sillycat.easynosql.model.User;
import com.sillycat.easynosql.service.UserService;
public class UserServiceRedisImpl implements UserService{
@Autowired
private RedisTemplate<String, String> template;
public User create(User user) {
String key = "user"+user.getUsername();
String id = UUID.randomUUID().toString();
//use hash structure to store the columns
template.opsForHash().put(key, "id", id);
template.opsForHash().put(key, "firstName", user.getFirstName());
template.opsForHash().put(key, "lastName", user.getLastName());
template.opsForHash().put(key, "username", user.getUsername());
template.opsForHash().put(key, "password", user.getPassword());
template.opsForHash().put(key, "role", user.getRole().getRole().toString());
//use set structure to store the users
template.opsForSet().add("user", key);
user.setId(id);
return user;
}
public User read(User user) {
String key = "user"+user.getUsername();
String existingRecord = (String) template.opsForHash().get(key, "id");
if (existingRecord == null) {
return null;
}
User returnUser = new User();
returnUser.setId((String) template.opsForHash().get(key, "id"));
returnUser.setFirstName((String) template.opsForHash().get(key, "firstName"));
returnUser.setLastName((String) template.opsForHash().get(key, "lastName"));
returnUser.setPassword((String) template.opsForHash().get(key, "password"));
returnUser.setUsername((String) template.opsForHash().get(key, "username"));
Role role = new Role();
role.setRole(Integer.valueOf((String) template.opsForHash().get(key, "role")));
returnUser.setRole(role);
return user;
}
public List<User> readAll() {
List<User> users = new ArrayList<User>();
Collection<String> fieldKeys = new HashSet<String>();
fieldKeys.add("id");
fieldKeys.add("firstName");
fieldKeys.add("lastName");
fieldKeys.add("username");
fieldKeys.add("password");
fieldKeys.add("role");
//fetch all the key from set
Collection<String> keys = template.opsForSet().members("user");
for (String key: keys) {
User user = new User();
//find the value with key/column name
user.setId((String) template.opsForHash().get(key, "id"));
user.setFirstName((String) template.opsForHash().get(key, "firstName"));
user.setLastName((String) template.opsForHash().get(key, "lastName"));
user.setPassword((String) template.opsForHash().get(key, "password"));
user.setUsername((String) template.opsForHash().get(key, "username"));
Role role = new Role();
role.setRole(Integer.valueOf((String) template.opsForHash().get(key, "role")));
user.setRole(role);
users.add(user);
}
return users;
}
public User update(User user) {
String key = "user"+user.getUsername();
String existingRecord = (String) template.opsForHash().get(key, "id");
if (existingRecord == null) {
return null;
}
template.opsForHash().put(key, "firstName", user.getFirstName());
template.opsForHash().put(key, "lastName", user.getLastName());
template.opsForHash().put(key, "role", user.getRole().getRole().toString());
return user;
}
public Boolean delete(User user) {
String key = "user"+user.getUsername();
template.opsForHash().delete(key, "id");
template.opsForHash().delete(key, "firstName");
template.opsForHash().delete(key, "lastName");
template.opsForHash().delete(key, "username");
template.opsForHash().delete(key, "password");
template.opsForHash().delete(key, "role");
String existingRecord = (String) template.opsForHash().get(key, "id");
Boolean existingMember = template.opsForSet().remove("user", key);
if (existingRecord != null) {
return false;
}
if (existingMember == false) {
return false;
}
return true;
}
}
And init data Service is as follow:
package com.sillycat.easynosql.dao.redis.init;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
public class InitRedisService {
@Autowired
private RedisTemplate<String, String> template;
public void init() {
// Delete existing ones
String key = "user"+"john";
template.opsForHash().delete(key, "id");
template.opsForHash().delete(key, "firstName");
template.opsForHash().delete(key, "lastName");
template.opsForHash().delete(key, "username");
template.opsForHash().delete(key, "password");
template.opsForHash().delete(key, "role");
key = "user"+"jane";
template.opsForHash().delete(key, "id");
template.opsForHash().delete(key, "firstName");
template.opsForHash().delete(key, "lastName");
template.opsForHash().delete(key, "username");
template.opsForHash().delete(key, "password");
template.opsForHash().delete(key, "role");
// Create new records
key = "user"+"john";
template.opsForHash().put(key, "id", UUID.randomUUID().toString());
template.opsForHash().put(key, "firstName", "John");
template.opsForHash().put(key, "lastName", "Smith");
template.opsForHash().put(key, "username", "john");
template.opsForHash().put(key, "password", "21232f297a57a5a743894a0e4a801fc3");
template.opsForHash().put(key, "role", "1");
template.opsForSet().add("user", key);
//overwrite set value here
key = "user"+"jane";
template.opsForHash().put(key, "id", UUID.randomUUID().toString());
template.opsForHash().put(key, "firstName", "Jane");
template.opsForHash().put(key, "lastName", "Adams");
template.opsForHash().put(key, "username", "jane");
template.opsForHash().put(key, "password", "ee11cbb19052e40b07aac0ca060c23ee");
template.opsForHash().put(key, "role", "2");
template.opsForSet().add("user", key);
}
}
Operation for Hash
template.opsForHash()
template.opsForHash().put
template.opsForHash().delete
Operation for Set
template.opsForSet()
template.opsForSet().add
template.opsForSet().remove
4. Spring Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.server}" p:port="${redis.port}"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>
<bean id="initRedisService" class="com.sillycat.easynosql.dao.redis.init.InitRedisService" init-method="init"/>
</beans>
There is no repository in redis, so one template to deal with the server is enough.
references:
http://krams915.blogspot.com/2012/02/spring-mvc-31-implement-crud-with_6764.html
1. Domain Layer
Map the Java classes of our domain to Hash Type in redis.
User id =1, username = karl, password = 111111, role = 1
redis>hmset user:1 id 1 username karl password 111111 role 1
redis>hgetall user:1
If you want to validate your redis command, you can try it here: http://try.redis-db.com/
So there is no annotation in domain layer. So my domain will be just the User and Role
public class User {
private String id;
private String firstName;
private String lastName;
private String username;
private String password;
private Role role;
...snip... getter and setter
public class Role {
private String id;
private Integer role;
...snip... getter and setter
2. Service Layer
package com.sillycat.easynosql.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import com.sillycat.easynosql.model.Role;
import com.sillycat.easynosql.model.User;
import com.sillycat.easynosql.service.UserService;
public class UserServiceRedisImpl implements UserService{
@Autowired
private RedisTemplate<String, String> template;
public User create(User user) {
String key = "user"+user.getUsername();
String id = UUID.randomUUID().toString();
//use hash structure to store the columns
template.opsForHash().put(key, "id", id);
template.opsForHash().put(key, "firstName", user.getFirstName());
template.opsForHash().put(key, "lastName", user.getLastName());
template.opsForHash().put(key, "username", user.getUsername());
template.opsForHash().put(key, "password", user.getPassword());
template.opsForHash().put(key, "role", user.getRole().getRole().toString());
//use set structure to store the users
template.opsForSet().add("user", key);
user.setId(id);
return user;
}
public User read(User user) {
String key = "user"+user.getUsername();
String existingRecord = (String) template.opsForHash().get(key, "id");
if (existingRecord == null) {
return null;
}
User returnUser = new User();
returnUser.setId((String) template.opsForHash().get(key, "id"));
returnUser.setFirstName((String) template.opsForHash().get(key, "firstName"));
returnUser.setLastName((String) template.opsForHash().get(key, "lastName"));
returnUser.setPassword((String) template.opsForHash().get(key, "password"));
returnUser.setUsername((String) template.opsForHash().get(key, "username"));
Role role = new Role();
role.setRole(Integer.valueOf((String) template.opsForHash().get(key, "role")));
returnUser.setRole(role);
return user;
}
public List<User> readAll() {
List<User> users = new ArrayList<User>();
Collection<String> fieldKeys = new HashSet<String>();
fieldKeys.add("id");
fieldKeys.add("firstName");
fieldKeys.add("lastName");
fieldKeys.add("username");
fieldKeys.add("password");
fieldKeys.add("role");
//fetch all the key from set
Collection<String> keys = template.opsForSet().members("user");
for (String key: keys) {
User user = new User();
//find the value with key/column name
user.setId((String) template.opsForHash().get(key, "id"));
user.setFirstName((String) template.opsForHash().get(key, "firstName"));
user.setLastName((String) template.opsForHash().get(key, "lastName"));
user.setPassword((String) template.opsForHash().get(key, "password"));
user.setUsername((String) template.opsForHash().get(key, "username"));
Role role = new Role();
role.setRole(Integer.valueOf((String) template.opsForHash().get(key, "role")));
user.setRole(role);
users.add(user);
}
return users;
}
public User update(User user) {
String key = "user"+user.getUsername();
String existingRecord = (String) template.opsForHash().get(key, "id");
if (existingRecord == null) {
return null;
}
template.opsForHash().put(key, "firstName", user.getFirstName());
template.opsForHash().put(key, "lastName", user.getLastName());
template.opsForHash().put(key, "role", user.getRole().getRole().toString());
return user;
}
public Boolean delete(User user) {
String key = "user"+user.getUsername();
template.opsForHash().delete(key, "id");
template.opsForHash().delete(key, "firstName");
template.opsForHash().delete(key, "lastName");
template.opsForHash().delete(key, "username");
template.opsForHash().delete(key, "password");
template.opsForHash().delete(key, "role");
String existingRecord = (String) template.opsForHash().get(key, "id");
Boolean existingMember = template.opsForSet().remove("user", key);
if (existingRecord != null) {
return false;
}
if (existingMember == false) {
return false;
}
return true;
}
}
And init data Service is as follow:
package com.sillycat.easynosql.dao.redis.init;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
public class InitRedisService {
@Autowired
private RedisTemplate<String, String> template;
public void init() {
// Delete existing ones
String key = "user"+"john";
template.opsForHash().delete(key, "id");
template.opsForHash().delete(key, "firstName");
template.opsForHash().delete(key, "lastName");
template.opsForHash().delete(key, "username");
template.opsForHash().delete(key, "password");
template.opsForHash().delete(key, "role");
key = "user"+"jane";
template.opsForHash().delete(key, "id");
template.opsForHash().delete(key, "firstName");
template.opsForHash().delete(key, "lastName");
template.opsForHash().delete(key, "username");
template.opsForHash().delete(key, "password");
template.opsForHash().delete(key, "role");
// Create new records
key = "user"+"john";
template.opsForHash().put(key, "id", UUID.randomUUID().toString());
template.opsForHash().put(key, "firstName", "John");
template.opsForHash().put(key, "lastName", "Smith");
template.opsForHash().put(key, "username", "john");
template.opsForHash().put(key, "password", "21232f297a57a5a743894a0e4a801fc3");
template.opsForHash().put(key, "role", "1");
template.opsForSet().add("user", key);
//overwrite set value here
key = "user"+"jane";
template.opsForHash().put(key, "id", UUID.randomUUID().toString());
template.opsForHash().put(key, "firstName", "Jane");
template.opsForHash().put(key, "lastName", "Adams");
template.opsForHash().put(key, "username", "jane");
template.opsForHash().put(key, "password", "ee11cbb19052e40b07aac0ca060c23ee");
template.opsForHash().put(key, "role", "2");
template.opsForSet().add("user", key);
}
}
Operation for Hash
template.opsForHash()
template.opsForHash().put
template.opsForHash().delete
Operation for Set
template.opsForSet()
template.opsForSet().add
template.opsForSet().remove
4. Spring Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.server}" p:port="${redis.port}"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>
<bean id="initRedisService" class="com.sillycat.easynosql.dao.redis.init.InitRedisService" init-method="init"/>
</beans>
There is no repository in redis, so one template to deal with the server is enough.
references:
http://krams915.blogspot.com/2012/02/spring-mvc-31-implement-crud-with_6764.html
发表评论
-
Stop Update Here
2020-04-28 09:00 331I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 491NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 377Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 381Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 351Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 439Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 449Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 392Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 475VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 401Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 496NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 438Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 346Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 262GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 463GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 336GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 322Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 330Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 320Serverless with NodeJS and Tenc ...
相关推荐
windows版本的Redis4 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), ...
redis-protocol-example 用 Go 来了解一下 Redis 通讯协议,本项目支持 Redis 的五种通讯协议,如下: 状态回复(status reply) 错误回复(error reply) 整数回复(integer reply) 批量回复(bulk reply) 多条...
redis-exampleredis-exampleredis-exampleredis-exampleredis-exampleredis-exampleredis-exampleredis-example 基于redis的幂等控制框架
4. `Redis on Windows.docx`、`Windows Service Documentation.docx`、`Redis on Windows Release Notes.docx`:这些文档提供了关于如何在Windows上安装、配置和管理Redis服务的详细指南,包括版本的发行说明,以及...
redis-cluster集群所需要的所有依赖包,包括redis4,redis-statu,ruby,ruby-gem,ruby-gem-redis等所有搭建redis-cluster集群所需要使用到的包。
4. **断开连接**:完成所有操作后,记得断开与Redis服务器的连接,释放资源。 5. **错误处理**:在使用过程中,应该添加适当的错误处理机制,以应对可能出现的网络问题、命令执行失败等情况。 ### 子VI和自定义...
1、redis_4.0.10-1_arm64.deb 银河麒麟v4+飞腾 安装包 2、自带服务启动 3、目录树 /opt/redis-4.0.10/ ├── bin │ ├── redis-benchmark │ ├── redis-check-aof │ ├── redis-check-rdb │ ├── ...
《log4net.redis:将日志流导向Redis的实践与解析》 在IT行业中,日志记录是系统监控和故障排查的重要环节。log4net作为.NET Framework中的一个强大的日志框架,广泛应用于各类项目中。然而,随着微服务架构的普及...
redis4免编译安装包,采用哨兵模式配置集群,修改配置可直接使用。redis4免编译集群安装包,redis4免编译集群安装包
4. `Redis on Windows.docx`、`Windows Service Documentation.docx`、`Redis on Windows Release Notes.docx`:这些文档提供了关于在Windows上安装、配置和管理Redis的详细信息,包括服务的创建、操作指南和版本...
redis_4_0_2打包送给您,作为一款优秀的二级缓存工具,一直是大家的青睐
4. **客户端连接**:使用Redis命令行客户端`redis-cli.exe`,在命令行输入`redis-cli`连接本地Redis服务。如果需要连接其他主机或端口,可以使用`redis-cli -h <host> -p <port>`。 5. **测试操作**:连接成功后,...
Redis Essentials is a fast-paced guide that teaches the fundamentals on data types, explains how to manage data through commands, and shares experiences from big players in the industry. We start off...
Redis Desktop Manager 2019.4 win x64 是一款专为Windows用户设计的图形化Redis数据库管理工具,特别适合于对Redis数据库进行可视化操作和管理。它提供了直观的界面,使得数据库的查看、编辑、操作变得更为简单。这...
《Redis Search4j 1.0.1:深入探索全文搜索与数据管理》 Redis Search4j是一个基于Redis的全文搜索引擎库,版本1.0.1提供了强大的搜索功能,为开发者在Redis数据存储中实现高效的搜索操作提供了便利。本文将深入...
4. **主从复制**:Redis支持主从复制,可以将一个Redis实例的数据复制到多个从实例,实现读写分离,提高系统的读取性能。 5. **事务**:Redis提供了简单的事务功能,允许多个操作在原子性下执行,确保数据一致性。 ...
redis4 和 redis5 的远程代码执行Redis远程代码执行redis4 和 redis5 的远程代码执行python 代码来自https://github.com/Ridter/redis-rce通过加载so文件,扩展新命令system.exec执行命令执行成功strings exp_lin.so...
redis配置文件redis.conf