Hi Thiago, This is what I've done, "Still don't completely understand tap
services though". Although the cache is being populated, I'm unable to
Inject it into the page and retrieve the results. Hopefully you can help me
to clarify how to use this as a service. 

//My app module
public class AppModule {

    public static void bind(ServiceBinder binder) {
        binder.bind(AutocompleteCache.class, AutocompleteCacheImpl.class);
        binder.bind(Scheduler.class);
    }

    @Startup
    public static void scheduleJobs(
            PeriodicExecutor executor,
            final Scheduler scheduler,
            final Logger logger) {
        try {
            scheduler.taskSecheduler(executor);
        } catch (Exception e) {
            logger.error("Error in Scheduler", e);
        }
    }

//Scheduler Class
public class Scheduler {

    private HibernateSessionSource sessionSource;
    private Session session;
    
    public Scheduler(Session session) {
        this.session = session;
    }

    public void taskSecheduler(PeriodicExecutor executor) throws Exception {
        try{
            ApplicationUserCache(executor);
        } catch (Exception e) {
            throw e;
        }
    }

    private void ApplicationUserCache(PeriodicExecutor executor) {
        executor.addJob(
            new IntervalSchedule(10000L),
            "Autocomplete Cache",
            new Runnable() {
                
                public void run() {
                    List<ApplicationUser> users =
session.createCriteria(ApplicationUser.class).list();
                    
                    AutocompleteCache autocompleteCache = new
AutocompleteCacheImpl();
                    autocompleteCache.store(users);
                }
                
            });
    }

//Cache interface
public interface AutocompleteCache {
    
    public void store(Collection<ApplicationUser> applicationUsers);
    
    public Collection<ApplicationUser> find(String search);
    
    public boolean cacheEmpty();
    
    public int cacheSize();

}


//Cache implementation 
public class AutocompleteCacheImpl implements AutocompleteCache {
    
    private static SortedMap<String, Set&lt;ApplicationUser>> cache;
    
    public AutocompleteCacheImpl() {
        cache = Collections.synchronizedSortedMap(new TreeMap<String,
Set&lt;ApplicationUser>>());
    }
    
    public void store(Collection<ApplicationUser> applicationUsers) {
        for(ApplicationUser applicationUser : applicationUsers) {
            store(applicationUser);
        }
    } 
    
    public Collection<ApplicationUser> find(String search) {
        System.out.println("here " + search);
        System.out.println("has cache " + cache.get(search));
        if(cache.get(search) != null) {
            Collection<ApplicationUser> _search = cache.get(search);

            if(_search.size() > 15) {
                return new ArrayList<ApplicationUser>(_search).subList(0,
15);
            }       
            return _search;

        }

        return Collections.emptySet();
    }
    
    public void store(ApplicationUser applicationUser) {
        populateCache(applicationUser, applicationUser.getName());
        populateCache(applicationUser, applicationUser.getEmail());
        populateCache(applicationUser, applicationUser.getLabel());
        populateCache(applicationUser, applicationUser.getName() + " " +
applicationUser.getEmail());
        
    }
    
    public boolean cacheEmpty() {
        if(cache.isEmpty()) {
            return true;
        }
        return false;
    }
    
    public int cacheSize() {
        return cache.size();
    }
    
    private void populateCache(ApplicationUser applicationUser, String name)
{
        int len = name.length();
        

        for (int i = 1; i <= len; i++) {
            String key = name.substring(0, i).toLowerCase();

            if(cache.containsKey(key)) {
                Set<ApplicationUser> exist = cache.get(key);
                if(!exist.contains(applicationUser)) {                
                    exist.add(applicationUser);
                }                
            } else {
                Set<ApplicationUser> _applicationUser = new
HashSet<ApplicationUser>();
                
                _applicationUser.add(applicationUser);
                cache.put(key, _applicationUser);
            }
         }
 
    }

}

//Page
@Inject
private AutocompleteCache autocompleteCache;

    public SelectModel onProvideCompletionsFromAuthorizer(String partial) {
        return selectModelFactory.create(new
ArrayList<ApplicationUser>(autocompleteCache.find(partial.toLowerCase())),
"Label");
    }



--
View this message in context: 
http://tapestry.1045711.n5.nabble.com/Cache-avaiable-to-all-user-sessions-tp5697890p5698116.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to