Package com.abiquo.server.core.task

Source Code of com.abiquo.server.core.task.CacheEntryRep

/**
* Copyright (C) 2008 - Abiquo Holdings S.L. All rights reserved.
*
* Please see /opt/abiquo/tomcat/webapps/legal/ on Abiquo server
* or contact contact@abiquo.com for licensing information.
*/
package com.abiquo.server.core.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

/**
* Repository to manage the redis-based {@link CacheEntryDAO}
*
* @author eruiz@abiquo.com
*/
@Component
public class CacheEntryRep
{
    @Autowired
    protected JedisPool jedisPool;

    @Autowired
    protected CacheEntryDAO entryDao;

    public CacheEntry save(CacheEntry entry)
    {
        Jedis jedis = jedisPool.getResource();
        Transaction transaction = jedis.multi();
        boolean discard = true;

        try
        {
            entryDao.save(entry, transaction);
            transaction.exec();
            discard = false;
        }
        finally
        {
            if (discard)
            {
                transaction.discard();
            }

            jedisPool.returnResource(jedis);
        }

        return entry;
    }

    public void delete(CacheEntry entry)
    {
        Jedis jedis = jedisPool.getResource();
        Transaction transaction = jedis.multi();
        boolean discard = true;

        try
        {
            entryDao.delete(entry, transaction);
            transaction.exec();
            discard = false;
        }
        finally
        {
            if (discard)
            {
                transaction.discard();
            }

            jedisPool.returnResource(jedis);
        }
    }

    public CacheEntry find(final String id)
    {
        Jedis jedis = jedisPool.getResource();

        try
        {
            return entryDao.findById(id, jedis);
        }
        finally
        {
            jedisPool.returnResource(jedis);
        }
    }
}
TOP

Related Classes of com.abiquo.server.core.task.CacheEntryRep

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.