[参考]redisは商品情報を格納し、注文や商品idを増やし、コメントをキャッシュします.


RedisKeyUtils
public class RedisKeyUtils {
	
	//  id  key
	public final static String ITEMS_ID = "ITEMS:ID";
	
	//     key
	public static final String ITEMS_SELLSORT = "ITEMS:SELLSORT";

	//    key
	public static String getItemKey(Long itemId){
       <span style="white-space:pre">	</span>     return "ITEMS:" + itemId;
        }

	//    key
	public static String getItemCommentKey(Long itemId){
	    return"ITEMS:COMMENT" + itemId;
        }

}
po
public class Comment {
	
	private long id;
	
	//    
	private String name;
	//    
	private Date date;
.....
public class Items {
private Integer id;

private String name;

private Float price;

private String pic;

private Date createtime;

private String detail;

JedisProTest
public class JedisProTest {
	private ApplicationContext applicationContext;

	//   jackson  json  
	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

	// redis  
	private JedisCluster jedisCluster;

	@Before
	public void init() {
		applicationContext = new ClassPathXmlApplicationContext(
				"classpath:applicationContext.xml");
		jedisCluster = (JedisCluster) applicationContext
				.getBean("jedisCluster");

	}

	//       id  
	@Test
	public void testGetItemsId() {
		Long item_id = jedisCluster.incr(RedisKeyUtils.ITEMS_ID);
		System.out.println(item_id);
	}

	//  hash       
	@Test
	public void testSaveItems() {

		jedisCluster.hset(RedisKeyUtils.getItemKey(1002l), "id", "3");
		jedisCluster.hset(RedisKeyUtils.getItemKey(1002l), "name", "  4");
		jedisCluster.hset(RedisKeyUtils.getItemKey(1002l), "price", "999.9");

	}

	//     id hash      
	@Test
	public void testGetItemsById() {

		Map<String, String> map = jedisCluster.hgetAll(RedisKeyUtils
				.getItemKey(1002l));
		System.out.println(map);
	}

	//  list        
	@Test
	public void testSaveItemsComment() throws JsonGenerationException,
			JsonMappingException, IOException {

		//          json
		Comment comment = new Comment();
		comment.setId(1l);
		comment.setName("    ,  !!");
		comment.setDate(new Date());

		//        json 
		String comment_json = OBJECT_MAPPER.writeValueAsString(comment);
		System.out.println(comment_json);

		//         redis
		jedisCluster
				.lpush(RedisKeyUtils.getItemCommentKey(1001l), comment_json);

		//       
		List<String> lrange = jedisCluster.lrange(
				RedisKeyUtils.getItemCommentKey(1001l), 0, -1);

		for (String content : lrange) {
			//  json    java  
			System.out.println(content);
			Comment comment_obj = OBJECT_MAPPER.readValue(content,
					Comment.class);
			System.out.println(comment_obj);
		}
	}

	//      
	//  sortedSet        
	@Test
	public void testItemsSellSort() {
		
		//       
		//  1001
		jedisCluster.zadd(RedisKeyUtils.ITEMS_SELLSORT, 100, "1001");
		//  1002
		jedisCluster.zadd(RedisKeyUtils.ITEMS_SELLSORT, 200, "1002");
		
		//         
		//  1001     
		jedisCluster.zincrby(RedisKeyUtils.ITEMS_SELLSORT, 1, "1001");
		
		//     10 
		Set<Tuple> zrevrangeWithScores = jedisCluster.zrevrangeWithScores(RedisKeyUtils.ITEMS_SELLSORT, 0, 9);
		for(Tuple content:zrevrangeWithScores){
			System.out.println(content.getElement()+"   "+content.getScore());
			
		}
		
	}
	
	//  key     
	@Test
	public void testSetKeyExpire() throws InterruptedException{
		
		//  test   1
		jedisCluster.set("test", "1");
		System.out.println(jedisCluster.get("test"));
		
		//       5 ,    
		jedisCluster.expire("test", 5);
		//       5 ,     
//		jedisCluster.pexpire("test", 5000);
		Thread.sleep(5000);
		
		System.out.println(jedisCluster.get("test"));
		
	}

}