Giter Site home page Giter Site logo

ggrandes / memoizer Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 11 KB

Agnostic Cache in Java. If you call the function again with the same arguments, memoize return value out from cache, instead of compute again.

License: Apache License 2.0

Java 100.00%
memoize cache java

memoizer's Introduction

memoizer

Agnostic Cache with Dynamic Proxies and Reflection in Java. Open Source Java project under Apache License v2.0, do not require any external lib.

Memoize a function makes it faster by caching the return values of the function; If you call the function again with the same arguments, memoize gives you the value out of the cache, instead of letting the function compute the value all over again.

Current Stable Version is 1.0.0


DOC

Usage Example

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;

import org.javastack.memoizer.Memoizer;

public class Example {
	/**
	 * Sample Interface to Memoize
	 */
	public static interface SampleInterface {
		public String hash(final String in) throws NoSuchAlgorithmException;
	}

	/**
	 * Sample Slow Implementation (MessageDigest with SHA-512)
	 */
	public static class SampleSlowImpl implements SampleInterface {
		private static final Charset UTF8 = Charset.forName("UTF-8");

		public String hash(final String in) throws NoSuchAlgorithmException {
			final MessageDigest md = MessageDigest.getInstance("SHA-512");
			final byte[] buf = md.digest(in.getBytes(UTF8));
			return DatatypeConverter.printBase64Binary(buf);
		}
	}

	public static void main(final String[] args) throws Throwable {
		final int TOTAL = (int) 1e6;
		final String TEST_TEXT = "hello world";
		//
		final SampleInterface slowCode = new SampleSlowImpl();
		final int cacheElements = 1024;
		final long cacheMillis = 1000; // 1 second
		final SampleInterface fast = (SampleInterface) Memoizer.memoize(
				slowCode, 
				cacheElements, 
				cacheMillis);
		//
		final long ts = System.currentTimeMillis();
		for (int i = 0; i < TOTAL; i++) {
			fast.hash(TEST_TEXT);
		}
		final long diff = System.currentTimeMillis() - ts;
		System.out.println("diff=" + diff + "ms" + "\t" + //
				fast.hash(TEST_TEXT));
	}
}

MAVEN

Add the dependency to your pom.xml:

<dependency>
    <groupId>org.javastack</groupId>
    <artifactId>memoizer</artifactId>
    <version>1.0.0</version>
</dependency>

Inspired in Perl Memoize, this code is Java-minimalistic version.

memoizer's People

Contributors

ggrandes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.