Search in sources :

Example 6 with RedisSerializer

use of org.springframework.data.redis.serializer.RedisSerializer in project newblogback by Zephery.

the class TagServiceImpl method updatetag.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Integer updatetag(Integer tId) {
    // 标签
    List<Tag> tags = tagMapper.getAllTags();
    JsonArray jsonArray = new JsonArray();
    for (Tag tag : tags) {
        List<Blog> blogs = tagMapper.getblogbytagid(tag.gettId());
        String str = tag.gettName() + " " + "(" + String.valueOf(blogs.size()) + ")";
        KeyAndValue keyAndValue = new KeyAndValue();
        keyAndValue.setKey(tag.gettName());
        keyAndValue.setValue(str);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("key", tag.gettId());
        jsonObject.addProperty("value", str);
        jsonArray.add(jsonObject);
    }
    boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {

        @Override
        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            connection.set(serializer.serialize("biaoqian"), serializer.serialize(jsonArray.toString()));
            return true;
        }
    });
    return tId;
}
Also used : KeyAndValue(com.myblog.model.KeyAndValue) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) Tag(com.myblog.model.Tag) Blog(com.myblog.model.Blog) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 7 with RedisSerializer

use of org.springframework.data.redis.serializer.RedisSerializer in project lamp-util by zuihou.

the class RedisAutoConfigure method setSerializer.

private void setSerializer(RedisConnectionFactory factory, RedisTemplate template, RedisSerializer<Object> redisSerializer) {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(redisSerializer);
    template.setValueSerializer(redisSerializer);
    template.setConnectionFactory(factory);
}
Also used : JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer)

Example 8 with RedisSerializer

use of org.springframework.data.redis.serializer.RedisSerializer in project blog_demos by zq2599.

the class RedisClient method set.

/**
 * 更新缓存中的对象,也可以在redis缓存中存入新的对象
 *
 * @param key
 * @param t
 * @param <T>
 */
public <T> void set(String key, T t) {
    byte[] keyBytes = getKey(key);
    RedisSerializer serializer = redisTemplate.getValueSerializer();
    byte[] val = serializer.serialize(t);
    RedisConnection redisConnection = getConnection();
    if (null != redisConnection) {
        try {
            redisConnection.set(keyBytes, val);
        } finally {
            releaseConnection(redisConnection);
        }
    } else {
        logger.error("1. can not get valid connection");
    }
}
Also used : RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 9 with RedisSerializer

use of org.springframework.data.redis.serializer.RedisSerializer in project blog_demos by zq2599.

the class RedisClient method set.

/**
 * 更新缓存中的对象,也可以在redis缓存中存入新的对象
 *
 * @param key
 * @param t
 * @param <T>
 */
public <T> void set(String key, T t) {
    byte[] keyBytes = getKey(key);
    RedisSerializer serializer = redisTemplate.getValueSerializer();
    byte[] val = serializer.serialize(t);
    RedisConnection redisConnection = getConnection();
    if (null != redisConnection) {
        try {
            redisConnection.set(keyBytes, val);
        } finally {
            releaseConnection(redisConnection);
        }
    } else {
        logger.error("1. can not get valid connection");
    }
}
Also used : RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 10 with RedisSerializer

use of org.springframework.data.redis.serializer.RedisSerializer in project study by youbl.

the class RedisConfiguration method initSerializer.

private static void initSerializer(RedisTemplate template) {
    // Key用StringRedisSerializer,避免写入Redis的Key和Value,前缀都会出现 \xAC\xED\x00\x05t\x00\x03
    RedisSerializer keySerializer = new StringRedisSerializer();
    template.setKeySerializer(keySerializer);
    template.setHashKeySerializer(keySerializer);
    RedisSerializer valSerializer = new JsonRedisSerializer<>(Object.class);
    // template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setValueSerializer(valSerializer);
    template.setHashValueSerializer(valSerializer);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer)

Aggregations

RedisSerializer (org.springframework.data.redis.serializer.RedisSerializer)11 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 RedisConnection (org.springframework.data.redis.connection.RedisConnection)3 Autowired (org.springframework.beans.factory.annotation.Autowired)2 ConditionalOnProperty (org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)2 Bean (org.springframework.context.annotation.Bean)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 StreamMessage (com.hedera.mirror.common.domain.topic.StreamMessage)1 TopicMessage (com.hedera.mirror.common.domain.topic.TopicMessage)1 BatchEntityListenerTest (com.hedera.mirror.importer.parser.record.entity.BatchEntityListenerTest)1 RedisKeyWrapper (com.jn.agileway.redis.core.key.RedisKeyWrapper)1 DelegatableRedisSerializer (com.jn.agileway.redis.core.serialization.DelegatableRedisSerializer)1 RedisKeySerializer (com.jn.agileway.redis.core.serialization.RedisKeySerializer)1 Blog (com.myblog.model.Blog)1 KeyAndValue (com.myblog.model.KeyAndValue)1 Tag (com.myblog.model.Tag)1 TestInstance (org.junit.jupiter.api.TestInstance)1 DataAccessException (org.springframework.dao.DataAccessException)1 RedisCallback (org.springframework.data.redis.core.RedisCallback)1