Redis(4)Learn from Example

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



猜你喜欢

转载自sillycat.iteye.com/blog/1553509