@Autowired 
    private RedisTemplate<String, Object> redisTemplate; 

    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    protected RedisSerializer<String> getRedisSerializer(){
        return redisTemplate.getStringSerializer();
    }/**
     * 添加用户
     */
    @Override
    public boolean addUser(final UserVo user,final String sessionId) throws Exception {
        boolean result=redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer =getRedisSerializer();
                byte[] key = serializer.serialize(sessionId);
                byte[] user1=null;
                try {
                    user1 = SerializableUtils.toByteArray(user);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                connection.append(key, user1);
                return true;
            }
        });
        return result;
    }
    
    /**
     * 查询用户
     */
    @Override
    public UserVo getUserVo(final String string) throws Exception {
        UserVo result=redisTemplate.execute(new RedisCallback<UserVo>() {
            @Override
            public UserVo doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer =getRedisSerializer();
                byte[] key = serializer.serialize(string);
                byte[] value = connection.get(key);
                if(value==null){
                    return null;}
                UserVo user=null;
                try {
                    user=(UserVo) SerializableUtils.fromByteArray(value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return user;
            }
        });    
        return result;
    }
    //添加字符串
    @Override
    public Boolean addString(final String key,final String value) throws Exception {
        boolean result=redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer =getRedisSerializer();
                
                byte[] k = serializer.serialize(key);
                byte[] v=null;
                try {
                    v = SerializableUtils.toByteArray(value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                connection.append(k, v);
                return true;
            }
        });
    
        return result;
    }
    
    /**
     * 查询字符串
     */
    @Override
    public String getString(final String key) throws Exception {
        String result=redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer =getRedisSerializer();
                byte[] keys = serializer.serialize(key);
                byte[] value = connection.get(keys);
                if(value==null){
                    return null;
                }
                String str=null;
                try {
                    str=(String) SerializableUtils.fromByteArray(value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return str;
            }
        });    
        return result;
    }
    
    /**
     * 查询字符串
     */
    @Override
    public Set<String> getKeys(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        return keys;
    }
    /**
     * 删除key
     */
    @Override
    public Boolean delete(final String key) {
        boolean result=redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer=getRedisSerializer();
                byte[] k = serializer.serialize(key);
                connection.del(k);
                return true;
            }
        });
        return result;
    }
  //更新字符串 @Override
public Boolean updateString(final String key,final String value) { if(key==null){ throw new NullPointerException("数据行不存在, key = " + key); } boolean result=redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer=getRedisSerializer(); byte[] k = serializer.serialize(key); byte[] v=null; try { v = SerializableUtils.toByteArray(value); connection.set(k, v); } catch (Exception e) { e.printStackTrace(); } return true; } }); return result; }
  //设置过期时间 @Override
public void setTime(final String key, final int liveTime) { redisTemplate.execute(new RedisCallback<Object>() { public Long doInRedis(RedisConnection connection) throws DataAccessException { if (liveTime > 0) { RedisSerializer<String> serializer=getRedisSerializer(); byte[] k = serializer.serialize(key); connection.expire(k, liveTime); } return 1L; } }); }