Package javax.cache

Examples of javax.cache.Cache


      // log.log(level, "log level: " + level.toString());

      // log.log(level, "clazz: " + clazz);
      // log.log(level, "id: " + Long.toString(id));

      Cache cache;
      HashMap<String, Object> object;
      try {
        // log.log(level, "1");
        CacheFactory cacheFactory = CacheManager.getInstance()
            .getCacheFactory();
        // log.log(level, "2");
        cache = cacheFactory.createCache(Collections.emptyMap());
        // log.log(level, "3");
        // Get the value from the cache.
        object = (HashMap<String, Object>) cache.get(clazz
            + Long.toString(id));
        // log.log(level, "4");
        if (object != null) {

          // log.log(level, "found " + clazz + " " + id +
          // " in memcache");
          return object;
        }
        // log.info("did not find " + clazz + " " + id +
        // " in memcache");
      } catch (CacheException e) {
        e.printStackTrace();
        // log.log(Level.WARNING, e.getMessage());
      }

      DatastoreService datastore = DatastoreServiceFactory
          .getDatastoreService();
      object = new HashMap<String, Object>();
      try {
        Entity e = datastore.get(KeyFactory.createKey(clazz, id));
        // properties
        for (Entry<String, Object> entry : e.getProperties().entrySet()) {
          Object value = entry.getValue();
          if (value instanceof Text) {
            object.put(entry.getKey(), ((Text) value).getValue());
          } else if (value instanceof String) {
            object.put(entry.getKey(), ((String) value));
          } else {
            object.put(entry.getKey(), value);
          }
        }
        object.put("id", Long.toString(e.getKey().getId()));

        try {
          // log.log(level, "1");
          CacheFactory cacheFactory = CacheManager.getInstance()
              .getCacheFactory();
          // log.log(level, "2");
          cache = cacheFactory.createCache(Collections.emptyMap());
          // log.log(level, "3");
          cache.put(clazz + id, object);
          // log.info("put " + clazz + " " + id + " in memcache");
        } catch (CacheException ce) {
          // exception handling
          ce.printStackTrace();
        }
View Full Code Here


    }

    @SuppressWarnings("unchecked")
    public HashMap<String, Object> get(final String clazz, final String name) {
      // check memcache first
      Cache cache;
      HashMap<String, Object> object = null;
      if (name == null || name == "") {
        return object;
      }
      try {
        cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
        // Get the value from the cache.
        object = (HashMap<String, Object>) cache.get(clazz + name);
        if (object != null) {
          return object;
        }
      } catch (CacheException e) {
      }

      DatastoreService datastore = DatastoreServiceFactory
          .getDatastoreService();
      //
      // stuff it in memcache
      object = new HashMap<String, Object>();
      try {
        Entity e = datastore.get(KeyFactory.createKey(clazz, name));
        // properties
        for (Entry<String, Object> entry : e.getProperties().entrySet()) {
          Object value = entry.getValue();
          if (value instanceof Text) {
            object.put(entry.getKey(), ((Text) value).getValue());
          } else if (value instanceof String) {
            object.put(entry.getKey(), ((String) value));
          } else {
            object.put(entry.getKey(), value);
          }
        }
        object.put("name", e.getKey().getName());

        try {
          cache = CacheManager.getInstance().getCacheFactory()
              .createCache(Collections.emptyMap());
          cache.put(clazz + name, object);
        } catch (CacheException ce) {
        }
      } catch (EntityNotFoundException enf) {
        // enf.printStackTrace();
      }
View Full Code Here

    }

    @SuppressWarnings("unchecked")
    public HashMap<Object, HashMap<String, Object>> getAll(String clazz) {
      // check memcache first
      Cache cache;
      HashMap<Object, HashMap<String, Object>> objects;
      try {
        cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
        // Get the value from the cache.
        objects = (HashMap<Object, HashMap<String, Object>>) cache
            .get("__ALL__" + clazz);
        if (objects != null) {
          return objects;
        }
      } catch (CacheException e) {
      }

      // //////

      DatastoreService datastore = DatastoreServiceFactory
          .getDatastoreService();
      objects = new HashMap<Object, HashMap<String, Object>>();

      Query q = new Query(clazz);

      PreparedQuery pq = datastore.prepare(q);

      for (Entity result : pq.asIterable()) {
        // iterate over each property
        HashMap<String, Object> obj = new HashMap<String, Object>();
        if (result.getKey().getName() != null) {
          obj.put("name", result.getKey().getName());
        } else {
          obj.put("id", result.getKey().getId());
        }
        for (Entry<String, Object> entry : result.getProperties()
            .entrySet()) {

          if (entry.getValue() instanceof Text) {

            obj.put(entry.getKey(),
                ((Text) entry.getValue()).getValue());
          } else {
            obj.put(entry.getKey(), entry.getValue());
          }
        }

        // handle both string and numeric id's
        Object key = result.getKey().getName() != null ? result
            .getKey().getName() : result.getKey().getId();

        objects.put(key, obj);
      }
      // if we had to go to the datastore, stash it in memcache for
      // the next time
      try {
        cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
        cache.put("__ALL__" + clazz, objects);
      } catch (CacheException ce) {
      }

      return objects;
    }
View Full Code Here

          } catch (RequestTooLargeException rtle) {
          }
        }

        try {
          Cache cache = CacheManager.getInstance().getCacheFactory()
              .createCache(Collections.emptyMap());
          cache.put((String) (clazz + id),
              (Map<String, Object>) properties);

          // update all cache
          Map<Object, Map<String, Object>> objects = (Map<Object, Map<String, Object>>) cache
              .get("__ALL__" + clazz);
          if (objects != null) {

            objects.put(id, properties);

            cache.put("__ALL__" + clazz, objects);
          }
        } catch (CacheException ce) {
          // ...
        }
        return e;
View Full Code Here

        e.setProperty(propertyName, new Text(value));
        datastore.put(e);
      }

      try {
        Cache cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put(propertyName, value);

        cache.put(clazz + e.getKey().getId(), hashMap);

        // update all cache
        HashMap<Object, HashMap<String, Object>> objects = (HashMap<Object, HashMap<String, Object>>) cache
            .get("__ALL__" + clazz);
        if (objects != null) {

          objects.put(id, hashMap);

          cache.put("__ALL__" + clazz, objects);
        }

      } catch (CacheException ce) {
        // ...
      }
View Full Code Here

        datastore.put(e);
      }

      try {
        Cache cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
        cache.put(clazz + e.getKey().getId(), properties);
      } catch (CacheException ce) {
        // ...
      }

      return e;
View Full Code Here

        }
        datastore.put(e);
      }

      try {
        Cache cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());

        cache.put(clazz + id, properties);

        // update the all cache
        HashMap<String, HashMap<String, Object>> objects = (HashMap<String, HashMap<String, Object>>) cache
            .get("__ALL__" + clazz);
        if (objects != null) {
          objects.put(Long.toString(id),
              (HashMap<String, Object>) properties);

          cache.put("__ALL__" + clazz, objects);
        }

      } catch (CacheException ce) {
        // ...
      }
View Full Code Here

        datastore.put(e);

      }

      try {
        Cache cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());

        cache.put(clazz + name, properties);

        // update the all cache
        HashMap<String, HashMap<String, Object>> objects = (HashMap<String, HashMap<String, Object>>) cache
            .get("__ALL__" + clazz);
        if (objects != null) {
          objects.put(name, (HashMap<String, Object>) properties);

          cache.put("__ALL__" + clazz, objects);
        }

      } catch (CacheException ce) {
        // ...
      }
View Full Code Here

      e.setProperty(propertyName, new Text(value));

      datastore.put(e);

      try {
        Cache cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());
        cache.put(clazz + e.getKey().getId(), value);
      } catch (CacheException ce) {
        // ...
      }

      return e;
View Full Code Here

        e.setProperty(propertyName, new Text(value));
        datastore.put(e);
      }

      try {
        Cache cache = CacheManager.getInstance().getCacheFactory()
            .createCache(Collections.emptyMap());

        HashMap<String, Object> object = new HashMap<String, Object>();
        object.put(propertyName, value);
        object.put("name", name);
        cache.put(clazz + name, object);

        // update the all cache
        HashMap<String, HashMap<String, Object>> objects = (HashMap<String, HashMap<String, Object>>) cache
            .get("__ALL__" + clazz);
        if (objects != null) {
          objects.put(name, object);

          cache.put("__ALL__" + clazz, objects);
        }

      } catch (CacheException ce) {
        // ...
      }
View Full Code Here

TOP

Related Classes of javax.cache.Cache

Copyright © 2018 www.massapicom. 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.