Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemcachedClient.get will return null sometimes, but it has a correct value actually. #58

Closed
sunchuanleihit opened this issue Jun 3, 2017 · 2 comments

Comments

@sunchuanleihit
Copy link

MemcachedClient.get most times return the correct value ,but will return null sometimes. how can i fix it?
I use Spring Boot.

My Configuration

@Configuration
public class UserMemcacheConfig extends BaseConfig{

    @Value("${memcache.user.host}")
    private String host;
    @Value("${memcache.user.port}")
    private int port;
    @Value("${memcache.user.poolSize}")
    private int poolSize;
    @Value("${memcache.user.connTimeout}")
    private long connTimeout;
    @Value("${memcache.user.opTimeout}")
    private long opTimeout;

    @Bean(name="userMemcacheClient")
    public MemcachedClient getMemcacheClient() throws IOException {
        return this.generateMemcacheClient(host, port, poolSize, connTimeout, opTimeout);
    }
}

My yml properties

memcache:
  default:
    host: 127.0.0.1
    port: 11211
    poolSize: 30
    connTimeout: 1000
    opTimeout: 500
  user:
    host: 10.0.0.248
    port: 9101
    poolSize: 30
    connTimeout: 1000
    opTimeout: 500
  note:
    host: 127.0.0.1
    port: 11211
    poolSize: 30
    connTimeout: 1000
    opTimeout: 500
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserRepository userRepository;

    @Resource
    private MemcachedClient userMemcacheClient;

    @Override
    public User getById(String userId){
        User user = userRepository.findOne(new ObjectId(userId));
        return user;
    }

    @Override
    public String getUserIdBySid(String sid) {
        System.out.println(sid);
        if(StringUtils.isEmpty(sid)) {
            return null;
        }
        String key = String.format(MemcacheKeys.userIdBySid, sid);
        try {
            System.out.println(key);
            String userId = userMemcacheClient.get(key);
            System.out.println(userId);
            if(StringUtils.isEmpty(userId)){
                User user = userRepository.findBySession(sid);
                if(user != null){
                    userId = user.getId().toString();
                    userMemcacheClient.set(key, 100000, userId);
                }
            }
            return userId;
        } catch (InterruptedException e1){
            System.out.println("InterruptedException: " + e1.getMessage());
        } catch (MemcachedException e2){
            System.out.println("MemcachedException: " + e2.getMessage());
        }catch (TimeoutException e3){
            System.out.println("TimeoutException: " + e3.getMessage());
        }
        return null;
    }

}

I use getUserIdBySid with same sid, but sometimes return null.

@killme2008
Copy link
Owner

Maybe it's evicted by memcached LRU, it's not an issue in xmemcached, if you found this happens, you can telnet to memcached and run get <key> command to test if the item is exists on memcached.

@sunchuanleihit
Copy link
Author

I use tencent cmem, cmem doesn`t support version command. Then the heartbeat will fail.
client.setEnableHeartBeat(false) can solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants